当我尝试用标题替换标签时,为什么 preg_replace return NULL?
Why does preg_replace return NULL when I'm trying to replace a tags with titles?
我正在尝试仅用文本替换链接并重新保存内容,但前提是它满足以下要求。但是,当我使用 preg_replace
函数时,它 returns NULL
即使我能够使用 array_replace($fullLinks,$linksRemoved)
.
将链接替换为文本
我删除了 array_replace
,因为我不知道如何将新数组放回内容中。
我已经浏览了所有其他“preg_replace
返回 NULL”的帖子,但无法找到适合我的解决方案。在搜索模式时,我假设 "hello I'm the title" 也是一种模式,或者我哪里出错了?
$post_id = 122232;
$content_post = get_post($post_id);
$content = $content_post->post_content;
$linksRemoved[] = array();
$FullLinks[] = array();
// preg_match_all('/<a.*href=\"(.*)\".*><\/a>/isU', $content, $matches);
// above only returns 11 links
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $content, $matches)) {
$avoidImages = array('.jpg', '.png', '.gif', '.jpeg', 'glossary');
foreach ($matches[0] as $match){
if($match == str_replace($avoidImages, '', $match)){
if (strpos($match, 'bush.com') == true || strpos(match, 'articles') == true){
$fullLinks[] = array('match' => $match );
$linksRemoved[] = array('links' => strip_tags($match) );
}
}
}
}
$linksRemoved = array_filter($linksRemoved);
$content = preg_replace($fullLinks, $linksRemoved, $content);
echo '<pre>';
var_dump($content);
echo '</pre>';
我已经尝试过使用 DOMDocument,但一直卡在 replaceChild 甚至跳过元素的地方,如下所示:
$linksRemoved
数组示例如下:
$fullLinks
数组是相同的,但单词是超链接的,因此它包含标记。
[0]=>
array(1) {
["match"]=>
string(114) "gluteus medius"
}
[1]=>
array(1) {
["match"]=>
string(174) "gluteal complex"
}
[2]=>
array(1) {
["match"]=>
string(130) "rectus abdominis"
}
AFAIK preg_replace 不适用于多级数组,更改这两行
$fullLinks[] = array('match' => $match );
$linksRemoved[] = array('links' => strip_tags($match) );
进入
$fullLinks[] = $match;
$linksRemoved[] = strip_tags($match);
我正在尝试仅用文本替换链接并重新保存内容,但前提是它满足以下要求。但是,当我使用 preg_replace
函数时,它 returns NULL
即使我能够使用 array_replace($fullLinks,$linksRemoved)
.
我删除了 array_replace
,因为我不知道如何将新数组放回内容中。
我已经浏览了所有其他“preg_replace
返回 NULL”的帖子,但无法找到适合我的解决方案。在搜索模式时,我假设 "hello I'm the title" 也是一种模式,或者我哪里出错了?
$post_id = 122232;
$content_post = get_post($post_id);
$content = $content_post->post_content;
$linksRemoved[] = array();
$FullLinks[] = array();
// preg_match_all('/<a.*href=\"(.*)\".*><\/a>/isU', $content, $matches);
// above only returns 11 links
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $content, $matches)) {
$avoidImages = array('.jpg', '.png', '.gif', '.jpeg', 'glossary');
foreach ($matches[0] as $match){
if($match == str_replace($avoidImages, '', $match)){
if (strpos($match, 'bush.com') == true || strpos(match, 'articles') == true){
$fullLinks[] = array('match' => $match );
$linksRemoved[] = array('links' => strip_tags($match) );
}
}
}
}
$linksRemoved = array_filter($linksRemoved);
$content = preg_replace($fullLinks, $linksRemoved, $content);
echo '<pre>';
var_dump($content);
echo '</pre>';
我已经尝试过使用 DOMDocument,但一直卡在 replaceChild 甚至跳过元素的地方,如下所示:
$linksRemoved
数组示例如下:
$fullLinks
数组是相同的,但单词是超链接的,因此它包含标记。
[0]=>
array(1) {
["match"]=>
string(114) "gluteus medius"
}
[1]=>
array(1) {
["match"]=>
string(174) "gluteal complex"
}
[2]=>
array(1) {
["match"]=>
string(130) "rectus abdominis"
}
AFAIK preg_replace 不适用于多级数组,更改这两行
$fullLinks[] = array('match' => $match );
$linksRemoved[] = array('links' => strip_tags($match) );
进入
$fullLinks[] = $match;
$linksRemoved[] = strip_tags($match);