簡體   English   中英

PHP preg_replace()使用

[英]Php preg_replace() using

我正在嘗試更改字符串的某些部分。 我為此使用了preg_replace()函數,但無法成功。

這是一個例子。

$str = "Suspendisse rutrum rhoncus leo vitae vehicula. <span class="sdsad"> Nunc nec dapibus nisi.</span> Donec facilisis mauris sapien, eget blandit enim  dignissim auctor. <span style="text-decoration: underline;" class="sadsad">Nullam a porta orci.</span>";

我需要讓零件以"<span"開頭,直到">"字符並將其轉為<p>或其他名稱。

$str = preg_replace('/<span.*>/', '<p>', $str);

我正在試圖解決這樣的問題,但它返回相同的值。

我需要做什么?

謝謝..

此正則表達式將為您解決問題。

$str = 'Suspendisse rutrum rhoncus leo </span> vitae vehicula. <span class="sdsad"> Nunc nec dapibus nisi.</span> Donec facilisis mauris sapien, eget blandit enim  dignissim auctor. <span style="text-decoration: underline;" class="sadsad">Nullam a porta orci.</span>';
$str_replaced = preg_replace('/<(\/{0,1})span[^>]*>/','<$1p>',$str);
echo $str_replaced;

可以選擇將尾部斜杠放入標記中,因此您只需要一個調用。

您在這里忘記了捕獲小組()

$str = preg_replace('/<span(.*)>/', '<p>', $str);

此處提供更多信息: http : //www.regular-expressions.info/brackets.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM