2011-06-27 34 views
0

文字列内の2つのサブ文字列の間にサブ文字列を取得しようとしています。このコードを使用して文字列内の合計サブ文字列を取得する

私は最初のサブストリングのみを取得しています。どうすればサブストリングをすべて手に入れることができますか?コード使用

おかげ Sateesh :

<?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>"; 
} 
?> 

答えて

0
$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'); 

これらの線に沿って何かが、これはあなたが正規表現を使用して探しているものを行う必要があり、あなたの関数に

0

ていることに取り入れてみてください。互いに[tag]が複数入れられていると機能しないことに注意してください。

<? 
$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

ありがとうございました..ありがとう – sateesh

関連する問題