Php 从正则表达式匹配结果中删除字符串(如果链接包含特定属性,则删除链接类)

Php 从正则表达式匹配结果中删除字符串(如果链接包含特定属性,则删除链接类),php,regex,str-replace,preg-replace-callback,Php,Regex,Str Replace,Preg Replace Callback,我试图让我的PHP代码从包含属性data class=“false”的链接中删除class=“something”,而不使用其他链接 $mytext = 'text text text <a href="/url" data-class="false" class="something"> link1</a> text text text <a href="/url" class="something">link2</a> text text t

我试图让我的PHP代码从包含属性data class=“false”的链接中删除class=“something”,而不使用其他链接

$mytext = 'text text text <a href="/url" data-class="false" class="something">
link1</a> text text text <a href="/url" class="something">link2</a> text text 
text <a href="/url" class="something" data-class="false">link3</a> text text 
text';

// This function is supposed to remove the class="something" string from matches.
function remove_class($matches) {
  return str_replace(' class="something"','', $matches[1]);
}

// Match any link that contains the data-class="false" string and do callback.
$mytext = preg_replace_callback('/<a.*?data-class="false".*?>/', 
'remove_class', $mytext);

echo $mytext;
$mytext='text文本
文本文本文本
文本';
//此函数用于从匹配项中删除class=“something”字符串。
函数删除类($matches){
返回str_replace('class=“something”',''$matches[1]);
}
//匹配包含data class=“false”字符串的任何链接并执行回调。
$mytext=preg_replace_回调('/text)
文本文本文本
文本文本文本
这应该行得通

$mytext = 'text text text <a href="/url" data-class="false" class="something">
link1</a> text text text <a href="/url" class="something">link2</a> text text 
text <a href="/url" class="something" data-class="false">link3</a> text text 
text';     
$mytext2=  str_replace('data-class="false" class="something"','data-class="false"', $mytext );
$mytext3=  str_replace('class="something" data-class="false"','data-class="false"', $mytext2 );
echo htmlentities($mytext3);
$mytext='text文本
文本文本文本
文本';
$mytext2=str_replace('data-class=“false”class=“something”','data-class=“false”',$mytext);
$mytext3=str_replace('class=“something”data class=“false”、'data-class=“false”、$mytext2);
回音文本(mytext3美元);

从您显示的代码中,我看不到使用
preg\u replace\u回调的理由,
preg\u replace
应该足够了

$mytext = 'text text text <a href="/url" data-class="false" class="something">
link1</a> text text text <a href="/url" class="something">link2</a> text text 
text <a href="/url" class="something" data-class="false">link3</a> text text 
text';

// Match any link that contains the data-class="false" and class="something" 
// Then removes class="something".
$mytext = preg_replace('/(<a.*?)(class="something")\s(.*?data-class="false".*?>)|(<a.*?)(data-class="false".*?)\s(class="something")(.*?>)/', 
'$1$3$4$5$7', $mytext);

echo $mytext;
$mytext='text文本
文本文本文本
文本';
//匹配包含数据class=“false”和class=“something”的任何链接
//然后删除class=“something”。
$mytext=preg_replace('/(文本)
文本文本文本
文本

正在进行的是
preg\u replace
匹配
class=“something”data class=“false”
data class=“false”class=“something”
。每个子模式(…)可以由$和子模式的编号引用。如果找到前三个子模式,则我们使用$1$3,而不使用$2,将匹配项替换为我们想要的子模式匹配项。由于子模式$4-$7未使用,因此,如果我们匹配$4-$7,则忽略它们,反之亦然。将
\s
保留在子模式之外,我们将被忽略正在删除和多余的空间。

您是否遇到错误?不要使用正则表达式解析HTML,而是使用特殊的解析器。您自己尝试过吗?这似乎会产生以下结果:text-text-link1 text-text-link3 text-text-text-text-text-text不,这行不通……查找“class=”something“data class=”false'不够健壮…链接标记中会有其他属性,如名称、id、rel等。我不能保证这些属性会像这样组合在一起。您自己尝试过吗?我想删除class=“something”部分,但前提是链接包含数据class=“false”。抱歉,我的反应有点快。它已更新。看起来很接近,但没有完全起作用…我只是尝试了一下,结果是:text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text将出现在data class=“false”…之前,因此无论顺序如何,它都需要工作。@ChrisPaddison似乎在行中有一个返回值不起作用。我添加了它是为了在
上更加可读。我已修复了上述代码。以下是工作链接:
$mytext = 'text text text <a href="/url" data-class="false" class="something">
link1</a> text text text <a href="/url" class="something">link2</a> text text 
text <a href="/url" class="something" data-class="false">link3</a> text text 
text';

// Match any link that contains the data-class="false" and class="something" 
// Then removes class="something".
$mytext = preg_replace('/(<a.*?)(class="something")\s(.*?data-class="false".*?>)|(<a.*?)(data-class="false".*?)\s(class="something")(.*?>)/', 
'$1$3$4$5$7', $mytext);

echo $mytext;
text text text <a href="/url" data-class="false">
link1</a> text text text <a href="/url" class="something">link2</a> text text 
text <a href="/url" data-class="false">link3</a> text text 
text