Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.85 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP : What is the best way to fetch string coming between some palceholder
  2. $msg    =   '> **
  3. >   <#1371620c479a4e98_>  Chauffeure -Emails  driver_age:72
  4. > driver_nationality:IN
  5. > driver_languages:French,
  6. > driver_name:Rinto George
  7. > driver_mobilenumber:9747 161861
  8. > driver_email:rinto@example.com[reply]I am a principal yes , I know
  9. > you[reply]  Fragen oder Hilfe benotigt?
  10. > 089-38168530 Secure Transmission of Sensitive Data by SSL
  11. >';
  12.        
  13. preg_match_all("/[reply](.*)[reply]/", $msg, $reply);
  14. print_r($reply)
  15.        
  16. Array
  17. (
  18.     [0] => Array
  19.         (
  20.             [0] => [reply]I am a principal yes , I know
  21.             [1] => [reply]  Fragen oder Hilfe benotigt?
  22.         )
  23.  
  24. )
  25.        
  26. <?php
  27.  
  28. $text = "[reply] something [reply] bla bla bla [reply] something else [reply]";
  29.  
  30. $matches = array();
  31. $lastMatch = 0;
  32. $matchCount = 0;
  33.  
  34. $search = "[reply]";
  35.  
  36. while(true) {
  37.     $thisMatch = strpos($text, $search, $lastMatch+1);
  38.     if($thisMatch === FALSE)
  39.         break;
  40.     if(++$matchCount % 2 == 0)
  41.     {
  42.         $lastMatch = $thisMatch;
  43.         continue;
  44.     }
  45.     //print substr($text, $lastMatch + strlen($search), $thisMatch - $lastMatch - strlen($search)) . "n";
  46.     array_push($matches, substr($text, $lastMatch + strlen($search), $thisMatch - $lastMatch - strlen($search)));
  47.     $lastMatch = $thisMatch;
  48. }
  49.  
  50. print_r($matches);
  51.  
  52. ?>
  53.        
  54. [mqudsi@iqudsi:~/Desktop]$ php reply.php
  55. Array
  56. (
  57.     [0] =>  something
  58.     [1] =>  something else
  59. )
  60.        
  61. $text = "[reply] something [/reply] bla bla bla [reply] something else [/reply]";
  62.  
  63. $matches = array();
  64. $end = -1;
  65.  
  66. while(true) {
  67.     $start = strpos($text, "[reply]", $end+1);
  68.     $end = strpos($text, "[/reply]", $start+1);
  69.     if($start === FALSE || $end === FALSE)
  70.         break;
  71.  
  72.     array_push($matches, substr($text, $start + strlen("[reply]"), $end - $start - strlen("[reply]")));
  73.     $lastMatch = $thisMatch;
  74. }
  75.        
  76. "~[reply](.*?)[/reply]~is"