Php 在文档中查找%string的变量匹配项并替换为后缀。%string.prefix,但在Url中找到时不是href

Php 在文档中查找%string的变量匹配项并替换为后缀。%string.prefix,但在Url中找到时不是href,php,regex,dom,pcre,google-translate,Php,Regex,Dom,Pcre,Google Translate,我将发送文本到谷歌翻译,但在字符串中,有无法翻译的变量 所以我必须给它们加上后缀和前缀 但如果它们位于锚href的URL内,则不执行任何操作 变量的可能格式为: @foo !bar %foobar {foobar} {foo}.bar !bar_baz %foo-baz 资料来源: > Hello Bob @foo <a href="/someurl/!foobar/!bar/word"> Word {foobar} </a> %foo someword !bar

我将发送文本到谷歌翻译,但在字符串中,有无法翻译的变量

所以我必须给它们加上后缀和前缀

但如果它们位于锚href的URL内,则不执行任何操作

变量的可能格式为:

@foo !bar %foobar {foobar} {foo}.bar !bar_baz %foo-baz
资料来源:

> Hello Bob @foo <a href="/someurl/!foobar/!bar/word"> Word {foobar} </a> %foo someword !bar_baz
你可以试试这个:

<a href[^>]*(?:(?:@|!|%|#)\w+|\{\w+\})[^>]*>\K|((?:@|!|%|#)\w+|\{\w+\})

php来源:

$re='/#foo someword#bar
';
$subst='$1';
$result=preg_replace('//m','',preg_replace($re,$subst,$str));
回声$结果;

谢谢你,我成功了,当我有时间的时候,我会发布你的答案中的解决方案。@GuruJR这就是我试图为这个解决方案提供的——替代方案。
function PregAddprefixSuffix($text){
      $pregpattern = '/(?<!href=\\")\{[a-zA-Z_0-9]+\}(\.\w+)?|(?<!href=\\")\%[a-zA-Z_0-9\-\w]+|(?<!href=\\")\@[a-zA-Z_0-9\-\w]+|(?<!href=\\")\#[a-zA-Z_0-9\-\w]+|(?<!href=\\")\![a-zA-Z_0-9\-\w]+/';
      $prefix = '<span class="notranslate">';
      $suffix = '</span>';
$result= preg_filter($pregpattern, $prefix.'$0'.$suffix, $text); }
 $p ='/(?<!href=\\")\{[a-zA-Z_0-9]+\}(\.\w+)?|(?<!href=\\")\%[a-zA-Z_0-9\-\w]+|(?<!href=\\")\@[a-zA-Z_0-9\-\w]+|(?<!href=\\")\#[a-zA-Z_0-9\-\w]+|(?<!href=\\")\![a-zA-Z_0-9\-\w]+/';
 preg_filter($p, $prefix.'$0'.$suffix, $text); 
$p = '/(?:<a.*?\\">)|([\@|\!|\#|\%|\{][a-zA-Z_0-9\-\w]*[\}]?([\}]?[\.][\w]*)?)/';
preg_match_all($p, $input_lines, $output_array)
 print_r($output_array);
function fixspan($text) {
$doc = new \DOMWrap\Document();
$doc->html($text);
$nodesem = $doc->find('em.notranslate')->contents()->unwrap();
$nodesspan = $doc->find('span.notranslate')->contents()->unwrap();
return $doc->find('body > p')->contents();  } 
<a href[^>]*(?:(?:@|!|%|#)\w+|\{\w+\})[^>]*>\K|((?:@|!|%|#)\w+|\{\w+\})
$re = '/<a href[^>]*(?:(?:@|!|%|#)\w+|\{\w+\})[^>]*>\K|((?:@|!|%|#)\w+|\{\w+\})/m';
$str = 'Hello Bob @foo <a href="/someurl/!foobar/!bar">Word {foobar} </a> #foo someword #bar
<a href="/abc/d>koramamam</a>';
$subst = '<span class="notranslate">$1<\\\\span>';

$result = preg_replace('/<span class="notranslate"><\\\\span>/m',"",preg_replace($re, $subst, $str));

echo $result;