2010-12-27 23 views
0

をインスタンスを1つだけ変更するには、私はこのようなhtmlコードの束を持っている:正規表現は、特定の文字列の前にofcharacter

<a href="http://example.com/project-a" title="Project A (test)">Project A (test</span></a> 
<a href="http://example.com/project-b" title="Project B (test)">Project B (test</span></a> 
<a href="http://example.com/project-c" title="Project C (test)">Project C (test</span></a> 
<a href="http://example.com/project-d" title="Project D (test)">Project D (test</span></a> 

あなたはそれを持って、各行の終わりに見ることができます:

(test</span></a> 
ただし、各ライン上の他の開口部、ブラケットがある見ることができるので、私はトンを推測

<span class="example"> 

を:

私はこの開口部ブラケットを変更したいのですが彼の基準は、閉鎖スパンタグを変更する必要がある前に、最初のオープニングブラケットです。

regexとphpでこれを行う方法はありますか?

答えて

1

まあ、それは常にこのようになります場合は、あなただけ使用することができます。

str_replace(' (test</span>', ' <span class="example">test</span>', $string) 

それとも、それは常にではない場合は、 "テスト" :)

preg_replace('/ \((.*?)<\/span>/', ' <span class="example">$1</span>', $string) 
1

使用preg_replace()機能:

$pattern = '(\*test</span></a>)'; 
$replaceWith = '<span class="example">'; 

$str = '<a href="http://example.com/project-a" title="Project A *test*">Project A *test</span></a> 
<a href="http://example.com/project-b" title="Project B *test*">Project B *test</span></a> 
<a href="http://example.com/project-c" title="Project C *test*">Project C *test</span></a> 
<a href="http://example.com/project-d" title="Project D *test*">Project D *test</span></a>'; 

$newStr = preg_replace($pattern, $replaceWith, $str); 
関連する問題