简体   繁体   中英

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

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) {

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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM