首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果str_ireplace()不符合php条件

如果str_ireplace()不符合php条件
EN

Stack Overflow用户
提问于 2021-12-10 01:29:26
回答 3查看 76关注 0票数 -2

我有一个使用str_ireplace()函数将逗号分隔的单词与文本匹配的代码:

代码语言:javascript
复制
$words = "word1, word2, word3";
$text = "This is word1 text word2";

if (str_ireplace(explode(', ', $words), '', $text) != $text) {
   /*any logic*/
}

请告诉我怎么做反向逻辑?那些。如果没有找到任何匹配的

else {}不起作用。

完整代码:

代码语言:javascript
复制
function get_matched_categories( $description_every ) {
    $correspondence_tables = get_field( 'correspondence_table', 'option' );
    $default_categories = get_field( 'default_categories', 'option' );
    if ( is_array( $correspondence_tables ) ) {
        $arr_cat = array();
        foreach ( $correspondence_tables as $child_correspondence ) {

            if ( str_ireplace( explode( ', ', $child_correspondence['keywords'] ), '', $description_every ) != $description_every ) {
                array_push( $arr_cat, get_cat_ID( $child_correspondence['category'] ) );
            } else {
                if ( is_array( $default_categories ) ) {
                    foreach ( $default_categories as $default_category ) {
                        array_push( $arr_cat, get_cat_ID( $default_category['default_category_name'] ) );
                    }
                } 
            }

        }
        return $arr_cat;
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-12-10 02:43:04

问题是,每当您不匹配$correspondence_tables的一个元素中的关键字时,您就会添加默认类别,但它可能与以后的元素匹配。

只有当$correspondence_tables中的关键字没有匹配时,才应该添加添加默认类别的代码。您可以通过检查$arr_cat是否为空来判断循环结束时是否发生了这种情况。

代码语言:javascript
复制
function get_matched_categories( $description_every ) {
    $correspondence_tables = get_field( 'correspondence_table', 'option' );
    $default_categories = get_field( 'default_categories', 'option' );
    if ( is_array( $correspondence_tables ) ) {
        $arr_cat = array();
        foreach ( $correspondence_tables as $child_correspondence ) {

            if ( str_ireplace( explode( ', ', $child_correspondence['keywords'] ), '', $description_every ) != $description_every ) {
                array_push( $arr_cat, get_cat_ID( $child_correspondence['category'] ) );
            }
        }
        if (empty($arr_cat) && is_array($default_categories)) {
            foreach ( $default_categories as $default_category ) {
                array_push( $arr_cat, get_cat_ID( $default_category['default_category_name'] ) );
            }
        } 
        return $arr_cat;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-12-10 01:35:59

代码语言:javascript
复制
#!/usr/bin/php
<?php
$words = "word1, word2, word3";
$text = "This is word1 text word2";

// this is the same code offered in the OP
// changed to make it easier to see the effect
$wordsArray = explode(', ',$words);
$replacedText = str_ireplace($wordsArray,'',$text);
var_dump($replacedText);
if ($replacedText !== $text) {
        echo "some logic".PHP_EOL;
} else {
        echo "no logic".PHP_EOL;
}

// remove all the non-word characters (since we only care about words) and
// split the array on white space and word boundaries
$textArray = preg_split('/[\s\b]+/',preg_replace('/[^\w ]/','',$text));

// use array_intersect to see if there is an intersection between the two arrays
$match = array_intersect($wordsArray,$textArray);
var_dump($wordsArray,$textArray,$match);
if (!empty($match)) {
        echo "some logic".PHP_EOL;
} else {
        echo "some different logic".PHP_EOL;
}

如果这是为了支持翻译,我建议使用gettext或框架

票数 1
EN

Stack Overflow用户

发布于 2021-12-11 12:42:40

你实际上是在试图替换某些东西,还是只需要匹配单词?

--如果只是为了匹配匹配将匹配并返回一个布尔值,以方便您的if/else;而将进一步允许您捕获所有匹配:

代码语言:javascript
复制
/* 
 The word-list needs to be a regular expression.
 \b = word-boundary to match whole words, i = case-insensitive 
*/

$words = '~\b(word1|word2|word3)\b~i';

$text = "This is word1 text word2";

// If you simply need to know if there's a match:

if(preg_match($words, $text)) {
    // yes
} else { }

// If you want to process all matched words:

if(preg_match_all($words, $text, $matches)) {
    // yes, these:
    var_dump($matches);
} else { }

--如果您需要替换替换有一个"count“变量,其中填充了替换的数量。(如果您不需要更换,您将使用_replace为扳手使用锤子。)

代码语言:javascript
复制
$text = preg_replace($words, '', $text, -1, $count);

if($count > 0) {
    // yes, text changed
} else { }

如果您希望将工作单词列表逗号分隔开,只需动态构建正则表达式:

代码语言:javascript
复制
$words_rx = '~\b' 
            . str_replace(', ', '|', $words)
            . '\b~i';

为什么在这里使用正则表达式?而不是滥用_replace,例如,我们可以在foreach($words as $word) { stripos($text, $word); }等基础上构建。在本例中,答案很简单:我们可以使用\b。你想要有一个可靠的方法来识别单词的边界和匹配整个单词。(因此a*void*ing awk*war*d子字符串匹配。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70298713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档