0

我如何将这两个 preg_replace 加在一起?我不知道该怎么做。

<?= preg_replace('/@(\w+)/', '<a href="https://www.twitter.com/$1">@$1</a>', stripslashes($row['tweet_text']))?>
<?= preg_replace('/#(\w+)/', '<a href="https://twitter.com/#!/search/$#$1">#$1</a>', stripslashes($row['tweet_text']))?>

它会做这两件事

stripslashes($row['tweet_text']
4

1 回答 1

3

你可以这样做:

$regex = array('/@(\w+)/','/#(\w+)/');
$replace = array(
  '<a href="https://www.twitter.com/$1">@$1</a>',
  '<a href="https://twitter.com/#!/search/$#$1">#$1</a>'
);
preg_replace($regex,$replace,stripslashes($row['tweet_text']));

检查文档: http: //php.net/manual/en/function.preg-replace.php

另外,这是一个很好的例子

于 2012-09-18T06:05:49.137 回答