[英]PHP regex - find and replace link title if the link contains specific string
[英]PHP regex to Find & Replace specific string
好,
我下面有一些這樣的文字:
<Jens>是我的名字。 我玩<足球>。 昨天我看過《史蒂芬》。 是的,我們肯定會在一起。
我只希望使用正則表達式(最好是)或任何其他方法以編程方式將'<'&'>'(包括<>)之間的所有文本加粗。 這是一種查找和替換。 因此,操作后的文本應為:
<Jens>是我的名字。 我玩<足球> 。 昨天我看過《史蒂芬》 。 是的,我們肯定會在一起 。
<?php
// header('Content-Type: text/plain; charset=utf-8');
$test = <<<TXT
< Jens > is my name. I play < Football >.
I saw < Steffy > Yesterday. Yeah, We will be < Together > For sure.
TXT;
$result = preg_replace_callback(
'/<[^>]+>/',
function($matches){
return '<b>' . htmlspecialchars($matches[0]) . '</b>';
},
$test
);
print_r($result);
?>
輸出:
<Jens>是我的名字。 我玩<足球> 。 昨天我看過《史蒂芬》 。 是的,我們肯定會在一起 。
您可以使用以下preg_replace
:
$repl = preg_replace('/(<[^>]*>)/', '<b>$1</b>', $str);
<b>< Jens ></b> is my name. I play <b>< Football ></b>. I saw <b>< Steffy ></b> Yesterday. Yeah, We will be <b>< Together ></b> For sure.
為了更好地了解和學習正則表達式以進行進一步的工作,您可以訪問以下鏈接
學習正則表達式
最好的和容易的之一,我最喜歡的是
對於初學者來說,非常好和容易的教程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.