PHP preg_replace从字符串中删除特定部分
我在理解PHP中的正则表达式方面有问题。我有img src:PHP preg_replace从字符串中删除特定部分,php,regex,Php,Regex,我在理解PHP中的正则表达式方面有问题。我有img src: src="http://example.com/javascript:gallery('/info/2005/image.jpg',383,550)" 需要从中构建: src="http://example.com/info/2005/image.jpg" 在没有javascript部分的情况下,如何从字符串中剪切第一部分和最后一部分以获得清晰的链接 现在我正在使用这个正则表达式: $con
src="http://example.com/javascript:gallery('/info/2005/image.jpg',383,550)"
需要从中构建:
src="http://example.com/info/2005/image.jpg"
在没有javascript部分的情况下,如何从字符串中剪切第一部分和最后一部分以获得清晰的链接
现在我正在使用这个正则表达式:
$cont = 'src="http://example.com/javascript:gallery('/info/2005/image.jpg',383,550)"'
$cont = preg_replace("/(src=\")(.*)(\/info)/","$1http://example.com$3", $cont);
输出为:
src="http://example.com/info/2005/image.jpg',383,550)"
使用
preg\u replace(“/src=\”\K.*(\/info[^']*)”[^\“]*/”,'http://example.com$1',$cont)
看
解释
--------------------------------------------------------------------------------
src= 'src='
--------------------------------------------------------------------------------
\" '"'
--------------------------------------------------------------------------------
\K match reset operator
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
info 'info'
--------------------------------------------------------------------------------
[^']* any character except: ''' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
[^\"]* any character except: '\"' (0 or more
times (matching the most amount possible))
作为替代解决方案,您还可以捕获
src=”http://example.com
通过匹配第1组中的协议进行零件更换,以便您可以在更换时使用它
(src="https?://[^/]+)/[^']*'(/info[^']*)'[^"]*
解释
--------------------------------------------------------------------------------
src= 'src='
--------------------------------------------------------------------------------
\" '"'
--------------------------------------------------------------------------------
\K match reset operator
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
info 'info'
--------------------------------------------------------------------------------
[^']* any character except: ''' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
[^\"]* any character except: '\"' (0 or more
times (matching the most amount possible))
捕获组1,匹配(src=“https?://[^/]+)/
,可选s,src=“http
,直到第一个://
/
匹配除[^']*'
之外的任何字符,然后匹配'
'
捕获组2,匹配(/info[^']*)
,后跟除/info
以外的任何字符。
匹配“[^”]*
,然后匹配除”
“
src="http://example.com/info/2005/image.jpg"
(src=\”(.*)(\/info[^']*)[^”]*
将与您拥有的字符串一起工作。它可以工作,谢谢:)它也可以工作,谢谢您的精确解释:)@PiciuU很高兴听到这个消息,请单击✓代码>在左边。谢谢你给我一个替代的解决方案:)最后,我对正则表达式的工作原理有了更多的了解