Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式替换特定字符串和onclick事件周围的标记_Php_Regex - Fatal编程技术网

Php 正则表达式替换特定字符串和onclick事件周围的标记

Php 正则表达式替换特定字符串和onclick事件周围的标记,php,regex,Php,Regex,这是我的绳子 <p>Testing is a key element to any application. <a href="software-testing.html" onclick="ga('send', 'event', 'internal', 'Node.js', 'Node.js');"> Link1 </a> is called Jasmine. In early 2000, there was a framework for testing

这是我的绳子

<p>Testing is a key element to any application. 
<a href="software-testing.html" onclick="ga('send', 'event', 'internal', 'Node.js', 'Node.js');"> Link1 </a> is called Jasmine. In early 2000, there was a framework for testing  JavaScript applications called JsUnit. Later this framework got upgraded and is now known as jasmine. </p>
<p>Jasmine helps in automated unit <a href="software-testing.html"> Link2 </a>
我想删除onclick事件和software-testing.html中唯一的标记,确切地说,它们与替换项匹配

我尝试了以下两个代码

 1) <a(.*?)software-testing.html(.*?)<\/a>
 2) \s*<a(.*?)software-testing.html|onclick="[^"]*"[^>]*>((?:(?!</a>).)*)</a>\s*
但在我的例子中,链接1和链接2被替换

这不管用。我已经搜索了好几天,尝试了很多其他的选择,但从未像预期的那样成功

仅对于Link1,在Link2中为“替换”而不是“替换”


请帮助我。

您可以使用以下正则表达式完成此操作

PHP


另一种方法是使用:


你的php代码在哪里?用regexia解析html不是一个好主意为什么不用javascript呢?链接1和链接2都有software-testing.html,你不想把它们都替换掉吗?也许可以将我想删除的唯一标记改为onclick事件和software-testing.html。如果您只是想删除onclick,那么您不能替换'onclick=ga'send'、'event'、'internal'、'Node.js'、'Node.js';',或者内容是动态的?请提供更多的例子。和/或考虑解析所有链接。我已经链接到OnCutt事件的值是不同的。对于Ex-12,我试图将其标记为:内容没有替换项。@RamGuru99没有理解您的意思。您能详细说明一下吗?我尝试过替换内容中的链接,但内容没有被替换检查屏幕截图只替换了,请帮助我。@RamGuru99-umm。。没有正确地理解你。你能提供你想要的输出的屏幕截图吗?我想用这个$1替换这个代码。我想要的输出结果是-在链接1处显示$1。请检查屏幕截图-我已从数据库中获取数据。不是从url。是的,这个方法允许你从任何地方获取数据——看我的例子就是使用一个字符串
<.*?\shref="software-testing.html"\sonclick.*?<\/.*?>
$regex = '/<.*?\shref="software-testing.html"\sonclick.*?<\/.*?>/';
$str = '<p>Testing is a key element to any application. 
<a href="software-testing.html" onclick="ga(\'send\', \'event\', \'internal\', \'Node.js\', \'Node.js\');"> Link1 </a> is called Jasmine. In early 2000, there was a framework for testing  JavaScript applications called JsUnit. Later this framework got upgraded and is now known as jasmine. </p>
<p>Jasmine helps in automated unit <a href="software-testing.html"> Link2 </a>';
$subst = 'Show me \$1';
$result = preg_replace($regex, $subst, $str);
echo $result;
<?php
require 'simple_html_dom.php';
$htmlString='<p>Testing is a key element to any application. 
<a href="software-testing.html" onclick="ga(\'send\', \'event\', \'internal\', \'Node.js\', \'Node.js\');"> Link1 </a> is called Jasmine. In early 2000, there was a framework for testing  JavaScript applications called JsUnit. Later this framework got upgraded and is now known as jasmine. </p>
<p>Jasmine helps in automated unit <a href="software-testing.html"> Link2 </a>';
$html = new simple_html_dom();
$html->load($htmlString);
$tags = $html->find('a[onclick][href=software-testing.html]');
$tags[0]->outertext=$tags[0]->innertext;
echo $html;