Mweston
Mweston

Reputation: 25

php substr with 'dot'

A simple question I hope, relating to a php substr command.

After doing a UNION on a MySQL query, it turns out that I need to change the behaviour of items in one of the joined tables. The only way I can do this is by identifying a url in a column, which starts with http://www.i to make it unique to the url other tables.

This works in theory and it does result in the required output with characters up to the 'dot' in www. Anything beyond this dot seems to be ignored so it's not working as soon as I insert the 'i'.

Here is the code ($f8x represents a url)

    <?php if (substr( $f8x, 0, 20 ) === "<a href=http://www.i") {
    echo "nothing";
}
    else {
        echo "<a href='http://www.example.com/xyz'>Show more ></a></div>";
    }
    ?>

Any help much appreciated

Thanks

Upvotes: 0

Views: 1038

Answers (2)

user2815792
user2815792

Reputation:

It looks like there is a typo in

<?php if (substr( $f8x, 0, 20 ) === "<a href=http://www.i") {

There should be 'or " before href, otherwise it's simply invalid.

Could you try with:

<?php if (substr( $f8x, 0, 21 ) === "<a href='http://www.i") {

Otherwise, a cleaner way to identify a URL would be to use the following, it just tries to get that URL (needle) anywhere in the string (haystack):

<?php if (strpos($f8x, 'http://www.i') !== false) {

Upvotes: 1

Scott
Scott

Reputation: 1922

Change your length of the substr to 19:

if (substr( "<a href=http://www.i", 0, 19 ) === "<a href=http://www.i")
    echo "nothing";
else 
    echo "<a href='http://www.example.com/xyz'>Show more ></a></div>";

Upvotes: 0

Related Questions