简体   繁体   English

正则表达式更改引号冲突

[英]Regex to change quotation mark conflict

I'm trying to create a PHP regex to filter my content on WordPress. I would like to transform quotation marks " " like that « » with non-breaking space.我正在尝试创建一个 PHP 正则表达式来过滤我在 WordPress 上的内容。我想将引号" "转换为带有不间断空格的« »

I also use Timber (TWIG) filter to achieve this.我还使用木材 (TWIG) 过滤器来实现这一点。

The problem is that this filter also changes url tags and image tags .问题是这个过滤器也改变url tagsimage tags

Example:例子:

<a href="http://www.example.com">My link</a>

<a href=« http://www.example.com »>My link</a>

What could I add in my regex to avoid this?我可以在我的正则表达式中添加什么来避免这种情况? Can I have some help please.我能帮忙吗?

functions.php函数.php

public function add_to_twig( $twig ) {
        $twig->addExtension( new Twig_Extension_StringLoader() );
        $twig->addFilter( new Timber\Twig_Filter( 'changemarks', 'changemarks' ) );
        return $twig;
    }

function changemarks( $text ) {
    $regex = '/"(.*?)"/';
    $subst = '«&#8239;$1&#8239;»';
    $result = preg_replace($regex, $subst, $text);

    return $result;
}

single.twig单身.twig

{{ post.content|changemarks }}

It's difficult to make regular expression in html my solution is just select the string by make some space before and after " for text很难在 html 中制作正则表达式我的解决方案只是 select 字符串,在"之前和之后为文本留出一些空间

    $regex = '/ "(.+?)" /';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM