Echo only results that match exact string (PHP)

I am echo’ing out results from a WP database plugin and I have managed to narrow down all the results required down to a post ID, it’s now displaying all url records related to that post ID. (Scroll down on this link to see results: http://universitycompare.com/analytics-test/)

This is what I’m using to echo the SQL database records:

Read More
  <?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>';
} ?>

I have a number of results being echoed that are related to the URL but are not specific, (see below for an example of whats being echoed). What I would like to do is only echo results that match this 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)

Above is an example, I don’t want to echo any results that don’t match the chosen URL exactly. Ideally, only the record that should be shown, should be the the results with (103) next to it. I don’t know the best possible way to do this.

I’ve looked a regex, but a little unsure? Is this something that I need to use?

Edit, (Answer to the above question, too low of a user score to answer own question):

      <?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>';
}


?>

Related posts

1 comment

  1. You may be looking for startsWith(). Try the following:

    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>';
    }
    

    implementation of startsWith() and endsWith() can be found here startswith-and-endswith-functions-in-php

    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);
    }
    

Comments are closed.