开发者

Getting total sub strings in the string

I am trying to get sub string in between two sub strings in string. By using t开发者_StackOverflow社区his code

I am getting first sub string only. Can you please say how can I get all sub strings?

Thanks Sateesh Using code:

<?php 
 function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;

    return substr($string,$ini,$len);

}
$fullstring = "[tag]php[/tag] [tag]java[/tag] "; 
$count = substr_count($fullstring, '[tag]');
for($i=0; $i<$count;$i++){
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");
echo "LineItems[$i]:".$parsed."<br>"; 
}
?>


$matches = array();
preg_match('#\[tag\]([^\[]+)\[/tag\]#', "[tag]php[/tag] [tag]java[/tag] ", $matches)
$matches == array("[tag]php[/tag] [tag]java[/tag] ", 'php','java');
array_shift($matches);
$matches == array('php','java');

Something along those lines, try incorporating that into your function


This should do what you're looking for using a regular expression. Do note that it won't work if you have multiple [tag]s nested within eachother.

<?
$fullstring = "[tag]php[/tag] [tag]java[/tag]";
$matches = array();
preg_match_all('@\[tag\](.*?)\[/tag\]@', $fullstring, $matches);
foreach ($matches[1] as $match) {
    // Do whatever you need to do with the matches here.
    echo "$match<br/>";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜