Hi

I have the following piece of code:

    $multiLinks = array();
    foreach ( $matches[0] as $k => $match ) {
        if ( $post->multiurl_token ) {
            if ( strpos($matches[2][$k], 'http://mysite.com') === false ) {
                $multiLinks[$matches[2][$k]] = $match.coes[3][$k];
            }
        }
        else {
            $multiLinks[$matches[2][$k]] = $matches[3][$k];
        }
    }

I want to add an additional check alongside the 'http://mysite.com' so that it tests against 'http://mysite.com' OR 'http://localhost'

In addition it would be brilliant if I could create a variable say $localhost which determines whether to check for 'http://localhost' and be say 0 for check and 1 for don't check. Then if $localhost=0 check against 'http://mysite.com' and 'http://localhost' or if $localhost=1 just check against 'http://mysite.com'

Sorry if this sounds gibberish and I've used all the wrong terminology - I've tried to explain it as best I can.

Can anyone help please?

Thank you

Mark

Member Avatar for LastMitch

@magicmarkuk

Sorry if this sounds gibberish and I've used all the wrong terminology - I've tried to explain it as best I can.

What are you trying to do?

Are you trying to match your website URL with a different URL?

In answer to your first question, just add or statements to your if stament

if ( strpos($matches[2][$k], 'http://mysite.com') === false ||  strpos($matches[2][$k], 'http://localhost') === false)

If I understand your second question correctly

    $multiLinks = array();
    $localhost = 'Assign 1 or 0 here'
    foreach ( $matches[0] as $k => $match ) {
        if ( $post->multiurl_token ) {
            if (($localhost == 0 && (strpos($matches[2][$k], 'http://mysite.com') === false || strpos($matches[2][$k], 'http://localhost') === false)) || ($localhost == 1 && strpos($matches[2][$k], 'http://mysite.com') === false)) {
                $multiLinks[$matches[2][$k]] = $match.coes[3][$k];
            }
        }
        else {
            $multiLinks[$matches[2][$k]] = $matches[3][$k];
        }
    }

Not sure where you are getting the 1 or 0 to assign to your locahost variable so can't provide that bit of code completely.

Thank you for your replies and help.

simplypixie - I have tried the first snippet of code

if ( strpos($matches[2][$k], 'http://mysite.com') === false ||  strpos($matches[2][$k], 'http://localhost') === false)

but for some strange reason it fails to recognise when either of these conditions are met?

The code I now have is:

        $multiLinks = array();
        foreach ( $matches[0] as $k => $match ) {
            if ( $post->multiurl_token ) {              

                if ( strpos($matches[2][$k], 'http://mysite.com') === false ||  strpos($matches[2][$k], 'http://localhost') === false) {

                    $multiLinks[$matches[2][$k]] = $matches[3][$k];
                }
            }
            else {
                $multiLinks[$matches[2][$k]] = $matches[3][$k];
            }
        }

I will carry on experimenting but I am really in the dark.

If anyone can help much appreciated.

Thanks
Mark

I've played and confused.

I have decided to post up what works and what doesn't an expanding the code.

The original code which worked by identifying http://localhost was as follows:

        $multiLinks = array();
        foreach ( $matches[0] as $k => $match ) {
            if ( $post->multiurl_token ) {
                if ( strpos($matches[2][$k], 'http://localhost') === false ) {
                    $multiLinks[$matches[2][$k]] = $matches[3][$k];
                }
            }
            else {
                $multiLinks[$matches[2][$k]] = $matches[3][$k];
            }
        }

Followed by a further piece of code a little later:

            if ( $data ) {
                $data = json_decode($data);

                if ( isset($data->token) && $data->token ) {

                    if ( isset($data->links) && $data->links ) {
                        $newlinks = array();
                        foreach ( $data->links as $link ) {
                            $newlinks[$link->original_url] = $link->short_url;
                        }

                        foreach ( $matches[0] as $k => $match ) {
                            if ( strpos($match, 'http://localhost') === false ) {
                                $newlink = $match;
                                $newlink = str_replace($matches[2][$k], $newlinks[$matches[2][$k]], $newlink);
                                $content = str_replace($match, $newlink, $content);
                            }
                        }
                    }

With http://localhost or http://mysite.com in both parts the URLs are recognised and treated accordingly.

The first permutation I tried is by adding OR into the first section of code:

        $multiLinks = array();
        foreach ( $matches[0] as $k => $match ) {
            if ( $post->multiurl_token ) {
                if ( strpos($matches[2][$k], 'http://mysite.com') === false ||  strpos($matches[2][$k], 'http://localhost') === false) {
                    $multiLinks[$matches[2][$k]] = $matches[3][$k];
                }
            }
            else {
                $multiLinks[$matches[2][$k]] = $matches[3][$k];
            }
        }

and leaving the second part unchanged.

This does not work and neither http://mysite.com or http://localhost are recognised.

However, if I change the second part to:

            if ( $data ) {
                $data = json_decode($data);

                if ( isset($data->token) && $data->token ) {

                    if ( isset($data->links) && $data->links ) {
                        $newlinks = array();
                        foreach ( $data->links as $link ) {
                            $newlinks[$link->original_url] = $link->short_url;
                        }

                        foreach ( $matches[0] as $k => $match ) {                   
                            if ( strpos($match, 'http://mysite.com') === false) {
                                $newlink = $match;
                                $newlink = str_replace($matches[2][$k], $newlinks[$matches[2][$k]], $newlink);
                                $content = str_replace($match, $newlink, $content);
                            }

                        }
                    }

i.e. change http://localhost to http://mysite.com it recognises anything that is http://mysite.com and treats it accordingly.

If I change the second part to include an OR i.e.

            if ( $data ) {
                $data = json_decode($data);

                if ( isset($data->token) && $data->token ) {

                    if ( isset($data->links) && $data->links ) {
                        $newlinks = array();
                        foreach ( $data->links as $link ) {
                            $newlinks[$link->original_url] = $link->short_url;
                        }

                        foreach ( $matches[0] as $k => $match ) {                   
                            if ( strpos($match, 'http://mysite.com') === false) ||  strpos($match, 'http://localhost') === false) {
                                $newlink = $match;
                                $newlink = str_replace($matches[2][$k], $newlinks[$matches[2][$k]], $newlink);
                                $content = str_replace($match, $newlink, $content);
                            }

                        }
                    }

I get the following error:
Parse error: syntax error, unexpected T_BOOLEAN_OR

Any help really appreciated.

Thanks
Mark

Member Avatar for LastMitch

Parse error: syntax error, unexpected T_BOOLEAN_OR

Which line does the error occur?

Sorry, I should have said.

The error appears on the line:

if ( strpos($match, 'http://mysite.com') === false) ||  strpos($match, 'http://localhost') === false) {

which is in the second part of the code.

Thanks
Mark

Member Avatar for LastMitch

@magicmarkuk

Parse error: syntax error, unexpected T_BOOLEAN_OR

The error mean there's something wrong with your if statement

Did you read and used what simplypixie post?

This is the correct statement:

if (($localhost == 0 && (strpos($matches[2][$k], 'http://mysite.com') === false || strpos($matches[2][$k], 'http://localhost') === false)) || ($localhost == 1 && strpos($matches[2][$k], 'http://mysite.com') === false))

If you are gonna to modify this code to match your code then you need to at least understand how to used the statement that simplypixie provided then you can modify it.

@LastMitch

I read what simplypixie posted and was using the simplest version of his answer i.e. checking everything worked before introducing the $localhost variable.

The code I posted in my first post was the first element of code. I then realised there was a second element that referred to http://localhost so I amended it in line with the post from simplypixie assuming (maybe incorrectly) that I could exchange the commands for

strpos($matches[2][$k]

to

strpos($match

Mark

Member Avatar for LastMitch

@magicmarkuk

I read what simplypixie posted and was using the simplest version of his answer i.e. checking everything worked before introducing the $localhost variable.

Not his but her

I then realised there was a second element that referred to http://localhost so I amended it in line with the post from simplypixie assuming (maybe incorrectly)

The error you were having before was an error in your if statement:

if ( strpos($match, 'http://mysite.com') === false) ||  strpos($match, 'http://localhost') === false)

I think simplypixie gave you the correct statement and simplypixie organized have neatly.

Not his but her
My apologies

Unfortunately, you now have me confused.

I used the code from simplypixie in the first section but when I came to the second section which is now producing the error the original code was slightly so I amended it in line with simplypixie's example of how to add in an OR.

I don't know how to progress this as my knowledge is obviously limited and I am trying my best to explain what I have done and what problems are occurring.

Mark

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.