preg_replace()是一个PHP函数,它使用正则表达式执行字符串替换。
(我搜索了,发现了很多有关相对于绝对URL转换的问题,但绝对与相对无关。) 我想从表单字段中获取输入,并最终获得相对URL。
phpstr_replace()和preg_replace()不使用html
当我尝试在功能中执行str_replace()或preg_replace()时,内容不会更改。 $ sadrzaj中的内容: $ sadrzaj ='asdasdasd 当我尝试在功能中进行或preg_replace()时,内容不会改变。 变量中的内容:: $sadrzaj 我的功能$sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>'; :: to_je_to() 值函数function to_je_to($content){ preg_match_all('/<img (.*?)\/>/', $content, $images); //print_r($images); if(!is_null($images)){ foreach($images[1] as $index => $value){ if(strpos($images[1], 'size-full') !== false){ //if(preg_match('/alt=""/', $value)){ $new_img = preg_replace('<img', "<img data-example", $images[0][$index]); $content = preg_replace($images[0][$index], $new_img, $content); } } } echo $content; // return no difference } -没有任何变化。 如果有to_je_to($sadrzaj);的类,请找到此图像,然后用"size-full"替换标签。 even<img data-example ...>或不起作用。 我做错了什么? thanks 您做错了的主要事情是用正则表达式解析HTML。您应该使用适当的DOM解析器,然后可以使用XPath查询来隔离所需的元素。 str_replace() ,关于您的原始代码的一些评论: preg_replace()永远不会是<?php $sadrzaj = '<p>asdasdasds</p><p><a href="http://www.example.com/wp-content/uploads/2018/11/image.jpg" itemprop="url" title="some title"><img alt="some alt title" class="alignnone size-full wp-image-243618" src="http://www.example.com/wp-content/uploads/2018/11/image.jpg" width="940" height="529"></a></p>asdasdasd<p>asdasd</p><h3>asdada</h3><p><a href="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" itemprop="url" title="some title 02"><img alt="some alt title 02" class="alignnone size-full wp-image-243653" src="http://www.example.com/wp-content/uploads/2018/11/image_02.jpg" width="940" height="529"></a></p><h3>asdasd</h3>'; function to_je_to($content) { $dom = new DomDocument; $dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xp = new DomXpath($dom); // this is complicated but is less fragile than just doing [contains(@class, 'size-full')] $nodes = $xp->query("//img[contains(concat(' ', normalize-space(@class), ' '), ' size-full ')]"); foreach ($nodes as $img) { $img->setAttribute("data-example", ""); } return $dom->saveHTML(); } echo to_je_to($sadrzaj); ,它永远是一个数组 尚不清楚为什么您要循环$images,然后基于null替换值 根本不使用组匹配$images[1]根本不需要,因此括号不需要在那里 lop中的the the the the the the the the call in the loop使用表达式周围的定义器搜索,因此两者都会失败,因为错误 $images[0]和(.*?)之间有很大的区别 您的功能(在您的preg_replace()和echo)中存在一些问题。此外,您需要在匹配return标签上的preg_match_all方面更加复杂。总体而言,您最好使用内置的DomDocument类,就像其他答案所提出的那样。如果您想继续使用正则表达式,则此功能应解决您遇到的问题。 preg_replace 3v4l.org上的demo
PHP将字符串替换为来自数组的值 我有一个字符串,例如: 你好
我有一个字符串,例如:<%First Name%> <%Last Name%> Hello <%First Name%> <%Last Name%> welcome
示例只有1个空白行之间的段落之间超过1个空白,并删除多余的白色空间
我有一个网站,需要用户编写简单的简短概况描述。一些用户正在编写带有大量空格和多余新线条的丑陋配置文件。这是很多工作...
Pretty直截了当;我似乎找不到有关PHP的确定性的任何确定性支持的命名反向:
这个是一个微不足道的例子,但是我想知道这个语法是否根本不支持。句法问题或不存在的功能?
将正则表达式模式数组与 preg_replace() 一起使用,但其中一个替换需要函数调用
具有安全性,也是已弃用的功能;在查找和替换中调用函数的最简单、最安全的方法是什么? 有四个可以插入的查找和替换模块...
解析包含 URL 和分隔数字的字符串,然后使用这些数字填充重复 URL 的数组
我在使用 PHP preg_replace 时遇到一些问题。 我有一个包含添加数字的图像网址的网址,例如: $url = http://example.com/111.jpg,121,122,123,124,125,126 最后的数字是...
我在使用 PHP preg_replace 时遇到一些问题。 我有一个包含添加数字的图像网址的网址,例如: $url = http://example.com/111.jpg,121,122,123,124,125,126 最后的数字...
这行不通。 我希望它用 ralph 替换数组值的每个实例。 相反,我得到了 $data 未更改的值。 有什么见解吗? $data =“里面有一只狗...
我有一个相对较大的元素数组,我想搜索字符串并替换任何匹配项。我目前正在尝试使用 preg_replace 和正则表达式来做到这一点: preg_replace(&qu...
如何调整 preg_replace() 以尊重查找替换对关联数组中的所有项目
我有这段代码,但似乎有问题,我无法让它工作。有趣的是,如果我将 /§0([^§]*)/ 替换为 /§2([^§]*)/ 或 /§1([^§]*)/ 所选的 id 有效,但其余的...
我有这段代码,但似乎有问题,我无法让它工作。有趣的是,如果我将 /§0([^§]*)/ 替换为 /§2([^§]*)/ 或 /§1([^§]*)/ 所选的 id 有效,但其余的...
我有一个文本文件,我提取了所有带有 http:// 的域名地址 现在我想替换所有的http://。在我的与“”匹配的数组中,但没有发生任何事情,我什至没有得到一个...
我的数据库有一个表,其中包含 1000 个术语及其定义。 我想打印这些定义并向每个已经是术语的单词添加一个跨度标签。 我用它来创建两个数组(模式...
用数组对同一字符串进行多次替换(可能是 preg_replace)
我需要用数组中的字符串替换某个字符串(问号)的多个实例。例如如果我要替换的字符串出现 3 次并且我的数组长度为 3,则第一个
我想将所有单词的第一个字符替换为大写。我可以用 ucwords 来做到这一点,但它不是 unicode 编码。我还需要设置分隔符。 这是示例文本。用于替换...中的每个单词
我正在尝试替换句子中的单词,即 你好世界什么 foo 到 你好世界什么foo 到目前为止有办法检测它: preg_replace('/( [A-Z][A-Z]+ )/sm','$1', $string); 但它没有任何作用...
我有一个小问题。我正在尝试替换句子中的单词,即 你好世界什么 经过 你好世界什么 到目前为止有办法检测它: preg_replace('/( [A-Z][A-Z]+ )/sm','$1', $strin...
PHP 使用 preg_replace :“分隔符不能是字母数字或反斜杠”错误
我正在尝试获取一串文本,如下所示: $string = "这个 (1) 是 (2) 我的 (3) 示例 (4) 文本"; 在括号内有正整数的每个情况下,我想替换...