We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,324 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Adding additional if statements

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

3
Contributors
10
Replies
19 Hours
Discussion Span
4 Months Ago
Last Updated
12
Views
magicmarkuk
Junior Poster in Training
65 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@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?

LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 350
Skill Endorsements: 47

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.

simplypixie
Practically a Master Poster
642 posts since Oct 2010
Reputation Points: 157
Solved Threads: 118
Skill Endorsements: 5

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

magicmarkuk
Junior Poster in Training
65 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

magicmarkuk
Junior Poster in Training
65 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Parse error: syntax error, unexpected T_BOOLEAN_OR

Which line does the error occur?

LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 350
Skill Endorsements: 47

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

magicmarkuk
Junior Poster in Training
65 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@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
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 350
Skill Endorsements: 47

@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

magicmarkuk
Junior Poster in Training
65 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

@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.

LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 350
Skill Endorsements: 47

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

magicmarkuk
Junior Poster in Training
65 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0962 seconds using 2.79MB