Php 比较字符串,使用较短的字符串

Php 比较字符串,使用较短的字符串,php,web-scraping,conditional-statements,Php,Web Scraping,Conditional Statements,我是一个初级程序员,试图创建一个简单的应用程序,只需刮取一个网站并返回值 我正试图做一些我认为很简单的事情,但在搜索和尝试之后,我已经放弃了仅仅问 使用scraper,我返回三个变量:$title1、$title2和$title3。所有的$title都来自我试图找到文章名称的不同方法。理想情况下,我只需要寻找一个,然后就可以完成,但有些网站存储数据的方式不同(有些通过meta标记、隐藏div、元素等) 我需要一种方法来执行以下伪代码: if $title1, $title2, $title3 !

我是一个初级程序员,试图创建一个简单的应用程序,只需刮取一个网站并返回值

我正试图做一些我认为很简单的事情,但在搜索和尝试之后,我已经放弃了仅仅问

使用scraper,我返回三个变量:$title1$title2$title3。所有的$title都来自我试图找到文章名称的不同方法。理想情况下,我只需要寻找一个,然后就可以完成,但有些网站存储数据的方式不同(有些通过meta标记、隐藏div、元素等)

我需要一种方法来执行以下伪代码:

if $title1, $title2, $title3 != null { // don't count a string if it is null

    $title1_stringlength = string_length($title1) //find string length of the $titles
    $title2_stringlength = string_length($title2)
    $title3_stringlength = string_length($title3)

    $realtitle = $lowestvalueofstringlength; // $realtitle gets whichever $title is shortest in length, not counting any null $title's

}
下面是我为什么需要这样做的一个例子:

echo $title1; //echoes "Exercise Daily"
echo $title2; //echoes "null"
echo $title3; //echoes "Exercise Daily - And More advice on SaveTheTwinkie.org"
$realtitle = $title1;//should be $title1 because it was shortest that wasn't null

//or a different example from another site

echo $title1; //echoes "Wow look at this Article Title!"
echo $title2; //echoes "null"
echo $title3; //echoes "Wow look at this Article Title! - from StupidArticles.tv"
$realtitle = $title1;//should be $title1 because it was shortest that wasn't null
因此,我的代码将查找字符串长度中最短的$title(不为null),并将值赋给$realtitle

谢谢你的帮助!如果你需要更多的细节,尽管问吧


编辑


这是我的完整代码:它一直工作到$title的其中一个是“”,然后$realtitle也变成“”


这个应该可以:

function real_title($titles) {
    /* Gets array of titles.
       Return the shortest that isn't null.
    */

    $min_length = strlen($titles[0]);
    $real_title = $titles[0];

    foreach ($titles as $single_title) {
       $title_length = strlen($single_title);
       if (($title_length < $min_length) && ($title_length > 0)) {
                $min_length = $title_length;
                $real_title = $single_title;
        }
    }

    return $real_title;
}
function real\u title($titles){
/*获取标题数组。
返回不为null的最短值。
*/
$min_length=strlen($titles[0]);
$real_title=$titles[0];
foreach($titles作为$single_title){
$title_length=strlen($single_title);
如果($title_length<$min_length)&($title_length>0)){
$min_length=$title_length;
$real_title=$single_title;
}
}
返回$real_title;
}
这个应该可以:

function real_title($titles) {
    /* Gets array of titles.
       Return the shortest that isn't null.
    */

    $min_length = strlen($titles[0]);
    $real_title = $titles[0];

    foreach ($titles as $single_title) {
       $title_length = strlen($single_title);
       if (($title_length < $min_length) && ($title_length > 0)) {
                $min_length = $title_length;
                $real_title = $single_title;
        }
    }

    return $real_title;
}
function real\u title($titles){
/*获取标题数组。
返回不为null的最短值。
*/
$min_length=strlen($titles[0]);
$real_title=$titles[0];
foreach($titles作为$single_title){
$title_length=strlen($single_title);
如果($title_length<$min_length)&($title_length>0)){
$min_length=$title_length;
$real_title=$single_title;
}
}
返回$real_title;
}

这里有一个变量,可以处理无限数量的字符串:

$shortest = NULL;

$shortestReduce = function ($string) use (&$shortest) {

    if ( ($string === "null") || !($len = strlen($string))) {
        return $shortest;
    }

    if (!isset($shortest) || $len < strlen($shortest)) {
        $shortest = $string;
    }

    return $shortest;
};

$shortestReduce($string1);
$shortestReduce($string2);
$shortestReduce($string3);
# ...

echo $shortest; # "Exercise Daily"
$shortest=NULL;
$shortestReduce=函数($string)使用(&$shortest){
如果($string==“null”)| |!($len=strlen($string))){
返回$1;
}
如果(!isset($shortest)|$len

此reduce允许您对生成一个结果的多个值应用相同的函数,这里的最短字符串不是
“null”

这里有一个变量,可用于无限数量的字符串:

$shortest = NULL;

$shortestReduce = function ($string) use (&$shortest) {

    if ( ($string === "null") || !($len = strlen($string))) {
        return $shortest;
    }

    if (!isset($shortest) || $len < strlen($shortest)) {
        $shortest = $string;
    }

    return $shortest;
};

$shortestReduce($string1);
$shortestReduce($string2);
$shortestReduce($string3);
# ...

echo $shortest; # "Exercise Daily"
$shortest=NULL;
$shortestReduce=函数($string)使用(&$shortest){
如果($string==“null”)| |!($len=strlen($string))){
返回$1;
}
如果(!isset($shortest)|$len

这个reduce允许您对多个值应用相同的函数来生成一个结果,这里最短的字符串不是
“null”
“Ehm”,这可能是一个愚蠢的问题,但是如果这三个标题都是“null”?@hakre..我不确定,我必须弄清楚它。谢谢你指出这一点,也许是个愚蠢的问题,但如果这三个标题都是“空的”,你会怎么做?@hakre.我不确定,我得弄清楚。感谢您指出,实际上您的算法中没有“映射”步骤,只有“减少”步骤。感谢您的代码,它工作得非常完美……有没有办法也不允许对0字的字符计数?@Jake:只需添加另一个条件,如
和(strlen($string))
@hakre…我再试了一次,尝试了$shortest的var_转储,值为空?这是我的代码:
确实有个错误,我把它修好了。因为它是空的,所以它已经是最短的。所以现在我用isset检查它是否为空,然后它将在第一个有效的输入字符串上初始化。实际上,在你的算法中没有“映射”步骤,只有“减少”步骤,感谢你的代码,它工作得很好……有没有办法也不允许对0字的字符计数?@Jake:只需添加另一个条件,如
和(strlen($string))
@hakre…我再试了一次,尝试了$shortest的var_转储,值为空?这是我的代码:
确实有个错误,我把它修好了。因为它是空的,所以它已经是最短的。所以现在我用isset检查它是否为NULL,然后它将在第一个有效的输入字符串上初始化。