仅回显与精确字符串匹配的结果(PHP)

时间:2015-09-20 20:33:03

标签: php mysql regex wordpress startswith

我正在回应WP数据库插件的结果,并且我已经设法将所需的所有结果缩小到一个帖子ID,它现在显示与该帖子ID相关的所有url记录。 (向下滚动此链接可查看结果:http://universitycompare.com/analytics-test/

这就是我用来回显SQL数据库记录的内容:

  <?php
echo "<strong>Stack Overflow Results</strong>";
echo "<br><div style='float:left; width:100%; padding:1em; background:#FFF; margin:2em 0;'>";
$lastDate = date('Y-m-d', strtotime('today - 30 days'));
$todayDate = date('Y-m-d');

$results = $wpdb->get_results( "SELECT * FROM `wp_statistics_pages` WHERE id=37 AND `date` BETWEEN '$lastDate' AND '$todayDate' ", ARRAY_A );
for($i=0;$i<count($results);$i++)
{
    echo "<span id='data-point'style='float:left;clear:left;' value='";
    echo $results[$i]['count'];
    echo "'>";
    echo get_site_url().''.$results[$i]['uri'].' (';
    echo $results[$i]['count'].')</span>';
} ?>

我有许多与URL相关的结果但不是特定的(请参阅下面的示例,了解回显的内容)。我想要做的只是与此网址匹配的回显结果:http://universitycompare.com/universities/anglia-ruskin-uni/

universitycompare.com/universities/anglia-ruskin-uni/(103)universitycompare.com/universities/anglia-ruskin-uni?offset=10(1)universitycompare.com/universities/anglia-ruskin-uni/?offset = 10(1)universitycompare.com/universities/anglia-ruskin-uni(1)

上面是一个例子,我不想回应任何与所选URL完全不匹配的结果。理想情况下,只应显示的记录应该是旁边(103)的结果。我不知道最好的方法。

我看起来像一个正则表达式,但有点不确定?这是我需要使用的东西吗?

编辑,(回答上述问题,用户得分太低,无法回答自己的问题):

      <?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

function endsWith($haystack, $needle) {
    // search forward starting from end minus needle length characters
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
}

echo "<strong>Stack Overflow Results</strong>";
echo "<br><div style='float:left; width:100%; padding:1em; background:#FFF; margin:2em 0;'>";
$lastDate = date('Y-m-d', strtotime('today - 30 days'));
$todayDate = date('Y-m-d');

$results = $wpdb->get_results( "SELECT * FROM `wp_statistics_pages` WHERE id=37 AND `date` BETWEEN '$lastDate' AND '$todayDate' ", ARRAY_A );
for($i=0;$i<count($results);$i++)
if (endsWith(get_site_url().''.$results[$i]['uri'], 'http://universitycompare.com/universities/anglia-ruskin-uni/'))

{
    echo "<span id='data-point'style='float:left;clear:left;' value='";
    echo $results[$i]['count'];
    echo "'>";
    echo get_site_url().''.$results[$i]['uri'].' (';
    echo $results[$i]['count'].')</span>';
}


?>

1 个答案:

答案 0 :(得分:1)

您可能正在寻找startsWith()。请尝试以下方法:

if (startsWith($results[$i]['count'], 'universitycompare.com/universities/anglia-ruskin-uni'))
{
    echo "<span id='data-point'style='float:left;clear:left;' value='";
    echo $results[$i]['count'];
    echo "'>";
    echo get_site_url().''.$results[$i]['uri'].' (';
    echo $results[$i]['count'].')</span>';
}

可以在startswith-and-endswith-functions-in-php

找到startsWith()和endsWith()的实现
function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
function endsWith($haystack, $needle) {
    // search forward starting from end minus needle length characters
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
}