在标签和str_replace()之间查找文本

时间:2019-01-20 01:13:28

标签: php regex

代码编辑

我想在任意两个html标签<tag></tag>之间查找文本并将其替换。 到目前为止,我在>这里的文本<

之间工作了
<?
$text = $_POST['text1'];
$one=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$two=array("&#x61;","&#x62;","&#x63;","&#x64;","&#x65;","&#x66;","&#x67;","&#x68;","&#x69;","&#x6A;","&#x6B;","&#x6C;","&#x6D;","&#x6E;","&#x6F;","&#x70;","&#x71;","&#x72;","&#x73;","&#x74;","&#x75;","&#x76;","&#x77;","&#x78;","&#x79;","&#x7A;","&#x41;","&#x42;","&#x43;","&#x44;","&#x45;","&#x46;","&#x47;","&#x48;","&#x49;","&#x4A;","&#x4B;","&#x4C;","&#x4D;","&#x4E;","&#x4F;","&#x50;","&#x51;","&#x52;","&#x53;","&#x54;","&#x55;","&#x56;","&#x57;","&#x58;","&#x59;","&#x5A;");
$preg = str_replace($one,$two,$text);
//this will remove text within brackets.
$text = preg_replace("/\>[^)]+\</",$preg, $text);
?>

我如何使其适用于所有html标签? 任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您应该在str_replace之前进行preg_match,例如:

$string=$_POST['text1'];
preg_match('#<tr>(.*?)</tr>#', $source, $string);
...
...
$str=str_replace($one,$two,$string[1]);