美国留学网站<img title="美国留学网站" alt="美国留学网站">
这段文字,怎么通过php的正则表达式函数 替换成
美国<a>留学</a>网站<img title="美国留学网站" alt="美国留学网站">
要求就是关键词在标签属性内的不加a标签。在标签以外的都加上。
<?php
@header ( 'Content-type: text/html;charset=GBK' );
$subject = '<img title="ç¾å½çå¦ç½ç«" alt="ç¾å½çå¦ç½ç«">ç¾å½çå¦ç½ç«<img title="ç¾å½çå¦ç½ç«" alt="ç¾å½çå¦ç½ç«">';
$pattern = "/(çå¦)(?![^\'\"><]*[\'\"])/";
$result = preg_replace ( $pattern, "<a>$1</a>", $subject );
echo $result;
?>追é®
$pattern = "/(çå¦)[^><]*<(?![^\'\"><]*>)/";
$result = preg_replace ( $pattern, "<a>$1</a><", $subject );
echo $result;
@header ( 'Content-type: text/html;charset=GBK' );
$subject = '<img title="ç¾å½çå¦ç½ç«" alt="ç¾å½çå¦ç½ç«">ç¾å½çå¦ç½ç«<img title="ç¾å½çå¦ç½ç«" alt="ç¾å½çå¦ç½ç«">';
$pattern = "/(çå¦)(?![^\'\"><]*[\'\"])/";
$result = preg_replace ( $pattern, "<a>$1</a>", $subject );
echo $result;
?>追é®
"ç¾å½çå¦ç½ç«"ç¾å½çå¦ç½ç«
å¦ææå¼å·ï¼æ¯å¦æå¼å§é£ç§ å°±ä¸è½æ·»å æ ç¾ï¼æ¯ä¸æ¯å ä¸title altè¿æ ·çéå¶æ´å¥½ä¸äºï¼
è¿æ å¯ä¸å¯ä»¥ç¨å¾®è®²è§£ä¸ä¸æ£åçå«ä¹ï¼è°¢è°¢
$pattern = "/(çå¦)[^><]*<(?![^\'\"><]*>)/";
$result = preg_replace ( $pattern, "<a>$1</a><", $subject );
echo $result;
温馨提示:答案为网友推荐,仅供参考