
Untitled
By: a guest on
Jul 29th, 2012 | syntax:
None | size: 1.85 KB | hits: 12 | expires: Never
PHP : What is the best way to fetch string coming between some palceholder
$msg = '> **
> <#1371620c479a4e98_> Chauffeure -Emails driver_age:72
> driver_nationality:IN
> driver_languages:French,
> driver_name:Rinto George
> driver_mobilenumber:9747 161861
> driver_email:rinto@example.com[reply]I am a principal yes , I know
> you[reply] Fragen oder Hilfe benotigt?
> 089-38168530 Secure Transmission of Sensitive Data by SSL
>';
preg_match_all("/[reply](.*)[reply]/", $msg, $reply);
print_r($reply)
Array
(
[0] => Array
(
[0] => [reply]I am a principal yes , I know
[1] => [reply] Fragen oder Hilfe benotigt?
)
)
<?php
$text = "[reply] something [reply] bla bla bla [reply] something else [reply]";
$matches = array();
$lastMatch = 0;
$matchCount = 0;
$search = "[reply]";
while(true) {
$thisMatch = strpos($text, $search, $lastMatch+1);
if($thisMatch === FALSE)
break;
if(++$matchCount % 2 == 0)
{
$lastMatch = $thisMatch;
continue;
}
//print substr($text, $lastMatch + strlen($search), $thisMatch - $lastMatch - strlen($search)) . "n";
array_push($matches, substr($text, $lastMatch + strlen($search), $thisMatch - $lastMatch - strlen($search)));
$lastMatch = $thisMatch;
}
print_r($matches);
?>
[mqudsi@iqudsi:~/Desktop]$ php reply.php
Array
(
[0] => something
[1] => something else
)
$text = "[reply] something [/reply] bla bla bla [reply] something else [/reply]";
$matches = array();
$end = -1;
while(true) {
$start = strpos($text, "[reply]", $end+1);
$end = strpos($text, "[/reply]", $start+1);
if($start === FALSE || $end === FALSE)
break;
array_push($matches, substr($text, $start + strlen("[reply]"), $end - $start - strlen("[reply]")));
$lastMatch = $thisMatch;
}
"~[reply](.*?)[/reply]~is"