Understanding PHP multi_curl_exec loops

In the PHP curl_multi_exec manual, a strange 3 loops are given as an example: do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } I was wondering why we needed so many while loops as opposed to a simple one such as: do { curl_multi_exec($master,$running); } while($running > 0); What I have found is mostly due to prevent making unnecessary calls to curl_multi_exec.
Read more...

PHP flush, sleep, and browsers…

If anything else doesn’t work, try this sample code:

 1 2 3 4 5 6 7 8 910111213141516
<?php
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
for ($i = 0; $i &lt; ob_get_level(); $i++) { ob_end_flush(); }
ob_implicit_flush(1);

// All you need is 256 spaces first
echo str_repeat(" ", 256); ob_flush();
for($i=0;$i&lt;10;$i++)
{
   echo $i,' ';
   ob_flush();
   sleep(1);
}

PHP strip_tags, while keeping the inner text

function strip_only($str, $tags) {
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
    return $str;
}

$str = '<p style="text-align:center">Paragraph</p><strong>Bold</strong><br/><span style="color:red">Red</span><h1>Header</h1>';

echo strip_only($str, array('p', 'h1'));
echo strip_only($str, '<p><h1>');

Credit: Steven (php.net)

Get the time duration from seconds

1234567891011121314151617181920212223242526 <?php /** * @param integer secs Number of seconds to convert * @return integer Time in weeks, days, hours, minutes, seconds * */ function duration($secs) { $vals = array( 'w' => (int) ($secs / 86400 / 7), 'd' => $secs / 86400 % 7, 'h' => $secs / 3600 % 24, 'm' => $secs / 60 % 60, 's' => $secs % 60 ); $ret = array(); $added = false; foreach ($vals as $key => $timeSC) { if ($timeSC > 0 || $added) { $added = true; $ret[] = $timeSC .
Read more...

Clean multiple new lines in a text

/**
 * Clean duplicates new line
 *
 */
  function cleanNewLine($text)  {
    $newLine = "\r\n";
    $strRes = '';

    $posNewLine = strpos($text, $newLine);
    if ($posNewLine===false) {
      $strRes = $text;

    } else {
      $startText = substr($text, 0, $posNewLine+2);
      $endText = substr($text, $posNewLine+2);
      $strRes .= $startText."";

      // Remove duplicate new line if exists
      $posNewLine = strpos($endText, $newLine);
      while($posNewLine===0) {
        $endText = substr($endText, $posNewLine+2);
        $posNewLine = strpos($endText, $newLine);
      }

      $strRes .= self::cleanNewLine($endText);
    }

    return $strRes;
  }

Split long words in a text

Recursive php function, to split long words:

/**
 * Cut words to max char, in a text
 *
 * @param    string text        Text to check
 * @param    integer maxChar    Max word length
 * @return   string             Verified text
 */
function checkWordLength($text, $maxChar=50) {

    $arrText = explode(' ', $text);
    foreach ($arrText as $key=>$t) {
      if (strlen($t)>$maxChar) {
        $arrText[$key] = chunk_split($t, $maxChar, ' ');
      }
    }

    return implode(' ', $arrText);
}

Use:

$text = checkWordLength($text);