【问题标题】:PHP compare titles with another titlePHP 将标题与另一个标题进行比较
【发布时间】:2014-10-19 12:49:51
【问题描述】:

我正在开发一个功能,它将帖子的标题与标题列表(我网站中的产品)进行比较。

它用于在我自己的网站上构建一个简单的广告系统,它会查看当前帖子的标题并将其与我网站中产品的标题进行比较。

如果匹配,系统需要从帖子标题中剪切产品标题字符串并删除其余部分。

示例:

当前标题:全新的山地自行车!

标题列表:

  1. 冰箱
  2. 山地自行车
  3. 书籍
  4. 笔记本电脑

所以我的系统需要观看标题:“全新的山地自行车!”,通过产品标题循环它,如果匹配“山地自行车”,它需要停止循环并切断“全新的”。

所以我只有字符串:“mountainbike”。

我的代码(我在 Wordpress 中构建):

    $current_title = get_the_title(); // "A brand new mountainbike!"
    $titles = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => 100 ) ); // List of titles
    if( $titles->have_posts() ) {
        while( $titles->have_posts() ) {
            $titles->the_post();
            $title = get_the_title(); // The product title from the list
            if( strpos( $current_title, $title ) ) {
                // Here I need to cut the product from the title
                $found = strpos( $current_title, $title );
                break;
            }
        }
    }

【问题讨论】:

  • 尝试使用 stripos(),它不区分大小写...并将其与 !== false 运算符匹配,因为返回 0 应该是正匹配
  • 看看strcasecmp
  • 感谢 cmets。我会努力的!
  • 或者您可以尝试使用preg_match('/mountainbike/i',$current_title,$match)$match 将保留您匹配的密钥。注意 i 使其不区分大小写
  • 谢谢,这可行,但是 $match 返回一个数组?

标签: php string wordpress loops strpos


【解决方案1】:

感谢 MoshMage,这段代码解决了我的问题。 $match 变量现在保存产品名称。

$current_title = get_the_title();
$titles = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => -1 ) );
if( $titles->have_posts() ) {
   while( $titles->have_posts() ) {
        $titles->the_post();
        $title = get_the_title();
        if( preg_match('/' . $title . '/i', $current_title, $matched ) ) {
            $match = $matched[0];
        }
    }
} 

【讨论】:

    【解决方案2】:

    strpos() 区分大小写,这就是为什么在“全新的山地自行车!”中找不到“山地自行车”的原因。

    将您的条件更改为:

    if( stripos( $current_title, $title) !== false ) {
    
    • 使用不区分大小写的 stripos
    • 并且还使用 !== false 因为如果你不这样做并且字符会在位置 0 上找到,那么你会认为没有匹配 - 有关更多信息,请参阅 "Return values" section of php manual

    完整代码:

     $current_title = get_the_title(); // "A brand new mountainbike!"
        $titles = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => 100 ) );      // List of titles
        if( $titles->have_posts() ) {
            while( $titles->have_posts() ) {
                $titles->the_post();
                $title = get_the_title(); // The product title from the list
                if( stripos($current_title, $title) !== false ) {
                    // Here I need to cut the product from the title
                    $found = $title;
                    break;
                }
            }
        }
    

    【讨论】:

    • 由于某种原因这不起作用。也许是因为我在循环中使用它?然后它下面的标题返回false,变量没有被设置?
    • 我认为你应该使用 if( stripos( $current_title, $title !== false ) ) { 而不是 if( stripos( $current_title, $title) !== false ) {
    • 我已经更新了我的答案 - 添加了整个代码(请参阅我还使用 $found = $title; 更改了行)并使用 )
    猜你喜欢
    • 1970-01-01
    • 2020-12-25
    • 2020-07-07
    • 1970-01-01
    • 2020-09-19
    • 2023-03-24
    • 2018-10-06
    • 1970-01-01
    • 2019-03-14
    相关资源
    最近更新 更多