0

Hello Guys im using this to Add http:// prefix to Url when missing. The problem is that he add it to the other Url's where i have http:// already.

foreach($result as $key => &$value)
    {

        if  (strpos($sample['Internetadress'], 'http://') === false){
            $sample['Internetadress'] = 'http://' .$sample['Internetadress'];
            }

    }
  1. i want that he doesnt edit it when it exist.
  2. i want that he doesnt add it when there is no URL.

Sorry about my english im from Germany :D

4
  • 1
    Possible duplicate of Add http:// prefix to URL when missing Commented May 3, 2018 at 8:19
  • The solution code to that dupe is exactly what the OP has lol. OP can you provide a data example of one which would cause a double http://. My first thoughts are that your url will look like http://https:// Commented May 3, 2018 at 8:22
  • Your code should cover your first point. Commented May 3, 2018 at 8:22
  • What about if its already https://? Commented May 3, 2018 at 8:35

2 Answers 2

1

Your code shoud cover your first point (when http exist don't change the URL), if not provide us a sample url that you want to modify.

For the second point you just make another check like this:

<?php
$sample['Internetadress']='www.example.com';

if  (strpos($sample['Internetadress'], 'http://') === false && trim($sample['Internetadress'])!==''){
    if(strpos($sample['Internetadress'], 'https://') === false){
        $sample['Internetadress'] = 'http://' .$sample['Internetadress'];
    }
}


echo $sample['Internetadress'];
Sign up to request clarification or add additional context in comments.

6 Comments

Should probably also be trim($sample['Internetadress']) !== '' in case it's got a random space or something in.
It works but when the existing URL begins with https:// hes still add http://. All the other problems are solved. Thx for this
Ah that's another thing you did not mention that you are going to have https://, you only said http and empty. Will modify it to make it work
Yes you right. Sorry about that, i recognized the problem with https:// right now.
I will wait for your modify. Thx
|
0

I think in this case a little regex is a good solution:

$urls = [
    'http://www.example.com',
    'foo.bar.com',
    'https://example.com',
    'www.example.com'
];

foreach ($urls as &$url) {
    $url = preg_replace('/^(?!http)/i', 'http://', $url);
}

The values in $urls after the loop are:

[
    "http://www.example.com",
    "http://foo.bar.com",
    "https://example.com",
    "http://www.example.com"
]

Also instead of using a foreach loop and passing the values by reference you could use a array_map() like this:

$urls = array_map(function($url) {
    return preg_replace('/^(?!http)/i', 'http://', $url);
}, $urls);

Comments

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.