我正在寻找一个PHP preg_replace()解决方案找到图像的链接,并用相应的图像标签替换它们.
找:
<a href="http://www.domain.tld/any/valid/path/to/imagefile.ext">This will be ignored.</a>
Run Code Online (Sandbox Code Playgroud)
用...来代替:
<img src="http://www.domain.tld/any/valid/path/to/imagefile.ext" alt="imagefile" />
Run Code Online (Sandbox Code Playgroud)
协议必须是http://,.ext必须是有效的图像格式(.jpg,.jpeg,.gif,.png,.tif),基本文件名称变为alt =""值.
我知道preg_replace()是这项工作的正确功能,但我很喜欢正则表达式,所以非常感谢任何帮助!谢谢!
这个问题与类似的情况有关,即使用php删除内联样式
那里的解决方案没有删除ie: <font face="Tahoma" size="4">
但是,假设我有一个内联样式和属性的混合包,如下所示:
<ul style="padding: 5px; margin: 5px;">
<li style="padding: 2px;"><div style="border:2px solid green;">Some text</div></li>
<li style="padding: 2px;"><font face="arial,helvetica,sans-serif" size="2">Some text</font></li>
<li style="padding: 2px;"><font face="arial,helvetica,sans-serif" size="2">Some text</font></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
需要什么regExp来实现这个结果?
<ul>
<li><div>Some text</div></li>
<li><font>Some text</font></li>
<li><font>Some text</font></li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我有一个实体数组,需要在一个大的sting中替换,但只有第一次出现(这就是为什么我使用preg_replace而不是str_replace),例如:
$entities = array();
$entities[0] = 'string1';
$entities[1] = 'string2';
$entities[2] = 'string2';
$entities[3] = 'Error String ('; ## this is the one that errors because of the bracket
$entities[4] = 'string4';
$entities[5] = 'string5';
foreach ($entities as $entity) {
$new_article = preg_replace('/' . $entity . '/', '##' . $key, $new_article, 1);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Warning (2): preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset XX
Run Code Online (Sandbox Code Playgroud)
使括号转义的最佳方法是什么,并且还可以转义可能在正则表达式中使用的任何其他字符.
谢谢
如何删除字符串末尾的所有非字母数字字符.例如:
Quick @# brown fox -
Quick @# brown fox##
Quick @# brown fox
Quick @# brown fox @$#
Run Code Online (Sandbox Code Playgroud)
一切都成了
Quick @# brown fox
Run Code Online (Sandbox Code Playgroud)
寻求可能使用preg_replace因为ereg_replace已弃用.
它也可以调整为允许字符串末尾的特定非字母数字字符,例如引号,感叹号,问号
我正在尝试匹配某个单词,并用某些文本替换单词的一部分,但保留单词的其余部分.我的理解是,在正则表达式模式的一部分中添加括号意味着当您使用时,括号内的模式匹配会被替换preg_replace()
出于测试目的,我用过:
$text = 'batman';
echo $new_text = preg_replace('#(bat)man#', 'aqua', $text);
Run Code Online (Sandbox Code Playgroud)
我只想用'aqua'代替'bat'来获得'aquaman'.相反,$new_text回应'aqua',忽略了'man'部分.
使用PHP我希望descr:从RIPE IP地址范围的whois记录开始获取该行的内容,因此它们应该看起来像这样:
% This is RIPE NCC's Routing Information Service
% whois gateway to collected BGP Routing Tables
% IPv4 or IPv6 address to origin prefix match
%
% For more information visit http://www.ripe.net/ris/riswhois.html
route: 53.0.0.0/8
origin: AS31399
descr: DAIMLER-AS Daimler Autonomous System
lastupd-frst: 2011-12-08 23:18Z 195.66.224.97@rrc01
lastupd-last: 2012-01-25 15:18Z 203.119.76.3@rrc00
seen-at: rrc00,rrc01,rrc03,rrc04,rrc05,rrc07,rrc10,rrc11,rrc12,rrc13,rrc14,rrc15,rrc16
num-rispeers: 98
source: RISWHOIS
Run Code Online (Sandbox Code Playgroud)
所以我应该得到DAIMLER-AS Daimler Autonomous System结果.
如何使用最少的代码执行此操作,我在$ whois中有记录.
<?php
$whois = shell_exec('whois -h riswhois.ripe.net ' . $ip);
?>
Run Code Online (Sandbox Code Playgroud) 这是一个示例内容(西班牙语)
Definición<br />
Elementos del fuego<br />
Clases de fuego, ¿Cómo evitar que comiencen las clases de fuego?<br />
Métodos de extinción<br />
Tipos de extintores portátiles<br />
Forma de usar los extintores portátiles<br />
Protección pasiva para el control de incendios, tipos de componentes.<br />
Procedimientos preventivos en caso de incendios en minería<br />
我想<br />用php 删除最后一次出现preg_replace().
我试过了
preg_replace('/<br \/>$/', '<br class="last" />', $content);
Run Code Online (Sandbox Code Playgroud)
但是不会吃饭.编译器出错了.
我该怎么办?
我需要让SQL查询只有两个词:男性或女性
我使用PHP函数返回:
return preg_replace( "/([male|female])/i", '', $data );
Run Code Online (Sandbox Code Playgroud)
目前,它消除了这些话,但我怎么删除绝对一切,只让
无论是男性还是女性?
我在这里错过了什么?
我想结合这2的preg_replace成一个单一的one.For例如每更换data-thumb-bg="与style="background-image: url('和之后,如果有.jpg将其替换为.jpg);.
$input = preg_replace('#data-thumb-bg="#s', 'style="background-image: url(', $input);
$input = preg_replace('#.jpg#s', '.jpg);', $input); //this must be executed only if a match has been found for above
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?
编辑
我想我错误地表达了自己
该.jpg应更换只有 data-thumb-bg=一直replaced.Like首先你更换data-thumb-bg,那么你更换.jpg后面的data-thumb-bg
亲切的问候
我有一个用PHP编写的Web应用程序.我想在用户评论中找到所有网址,并将其更改为可点击的链接.我搜索了很多网站和页面,发现下面的解决方案(不幸的是我没有找到它的参考链接):
<?php
function convert($input) {
$pattern = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@';
return $output = preg_replace($pattern, '<a href="http$2://$4">$0</a>', $input);
}
?>
Run Code Online (Sandbox Code Playgroud)
这段代码完全归功于它的作者.但我发现其中有一个我无法解决的错误.
如果检测到的URL开始小号字母(无HTTPS),该HREF值不会有š性格和HTTP将变更为HTTPS,而内部的文字是正确的.
示例:
source.com >><a href="https://ource.com">source.com</a>
你有解决这个bug的任何解决方案吗?