Php 正则表达式前面不带href=";
因此,我在WordPress环境中围绕youtube链接添加了[embed][/embed],因为如果您在后端使用与普通内容编辑器不同的内容输入字段,它将不会自动执行此操作(即使您应用了内容过滤) 因此,我发现这个正则表达式非常适合我的应用程序:Php 正则表达式前面不带href=";,php,regex,wordpress,youtube,embed,Php,Regex,Wordpress,Youtube,Embed,因此,我在WordPress环境中围绕youtube链接添加了[embed][/embed],因为如果您在后端使用与普通内容编辑器不同的内容输入字段,它将不会自动执行此操作(即使您应用了内容过滤) 因此,我发现这个正则表达式非常适合我的应用程序: $firstalinea = preg_replace('/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\
$firstalinea = preg_replace('/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i', '[embed]https://www.youtube.com/watch?v=$2[/embed]', $firstalinea);
除了一件事。如果有人将链接放在YouTube视频上而不是想要嵌入它,它也会替换,然后链接就不再起作用了
<a href="https://youtu.be/xxxxxx">Link</a>
那么,如果前面有href=“”,如何使正则表达式不起作用
谢谢!解决了这个问题:
$re = '/(?<!href=\")(http:\/\/|https:\/\/)(?:www\.)?youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i';
$firstalinea = preg_replace($re, '[embed]https://www.youtube.com/watch?v=$3[/embed]', $firstalinea);
$re='/(?使用preg_match进行测试,看看它前面是否有hrefhow?我也做了这一步,它可以与正则表达式测试器一起工作,但抛出php7错误:(?错误:preg_replace():未知修饰符“('