1

i'm new at this forum and php. I want to show some info when the script detects one or more words in the postname (Wordpress) In my exapmle like to dispaly extra info when Omnik + reset or wifi is detected.

I like to know how i can simplify the following code:

    $url = "www.myurl.nl/postname"
    if (strpos($url, 'omnik' )!==false){
    echo "Omnik";
    }
    else if (strpos($url, 'reset' )!==false){
    echo "Reset"; 
    }
    else if (strpos($url, 'wifi' )!==false){
    echo "Wifi";
    }
    else {
    echo "No Omnik,Reset or Wifi there";
    }

At this moment i can only show the extra info when the word "Omnik" is detected. Example: https://geaskb.nl/omnik shows the extra info, but https://geaskb.nl/omnik-reset and https://geaskb.nl/omnik-wifi should show the info too, while https://geaskb.nl/solaredge shouldn't show the info.

Hope you get what i mean.

====== Added 20:00 ========

Hi All, thanks for the ansewers.
I should have be clear the 1st time i guess.

This is the code i use now:
    // Verkrijg URL incl. subdir.
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
    $geturl= "https"; 
    else
    $geturl = "http"; 
  
    // Here append the common URL characters. 
    $geturl .= "://"; 
  
    // Append the host(domain name, ip) to the URL. 
    $geturl .= $_SERVER['HTTP_HOST']; 
  
    // Append the requested resource location to the URL 
    $geturl .= $_SERVER['REQUEST_URI']; 


    if (strpos($_SERVER['REQUEST_URI'], "omnik" )!==false){
    echo "Omnik in url";
    }
    else {

    echo "Geen Omnik in $geturl";

    }
 ============= 20:30u ==================
Problem solved!! stripos solved the problem!
Thanks for all your help!
2
  • You could use a loop Commented Jul 1, 2020 at 16:45
  • I see you solved your problem, could you post your solution as an answer? Or if it's below can you accept the correct answer? Commented Sep 24, 2020 at 20:19

1 Answer 1

2

One way for doing it through foreach loop

    <?php

$url = "www.myurl.nl/postname";

$needles =  ['omnik', 'reset', 'wifi']; // Add more if needed

foreach($needles as $needle){
    if (strpos($url, $needle )!==false){
        echo $needle; 
    }
}
?>
Sign up to request clarification or add additional context in comments.

7 Comments

Except ucwords should be used on the echo. Also your solution doesn't handle the final else.
Yes, that depends on how you want to print it.
Noble attempt, im just not a fan of handing source code to OP's without their own demonstrated effort
On the subject of case-sensitivity, consider using stripos rather than strpos if you need to match upper- and lower-case combinations of characters in the URL (for example, if you want to to match www.myurl.nl/Omnik as well as www.myurl.nl/omnik).
@GetSet -> i can understand what you mean, but, i'm a dutch newbee trying to learn some php, so many thanks taking the time to answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.