user2534787
user2534787

Reputation: 115

Something like SQL "LIKE" but in PHP

I have few url in my database, it goes like:

id url

1 http://test.com/embed-990.html
2. http://test2.com/embed-011.html
3. http://test3.com/embed-022.html

How I could make a simple php code if one of url doesn't exist in database, just to load another? I need to check these url by domain as well.

For example something like this:

if($data['url'] == "test.com") {
 echo "my embed code number 1";
} elseif($data['url'] == "test2.com") {
 echo "my another embed code";
}

Upvotes: 0

Views: 99

Answers (3)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

You can use Regx

$domains = ['test.com', 'test1.com', 'test20.com'];
foreach( $domains as $domain ){
    if(preg_match('/test([0-9]*)\.com/', $domain, $match)){
        echo "my embed code number {$match[1]}\n";
    }
}

Outputs:

my embed code number 
my embed code number 1
my embed code number 20

you can test it here

http://sandbox.onlinephpfunctions.com/code/1d4ed1d7505a43b5a06b5ef6ef83468b20b47799

For the regx

  • test matches test literally
  • ([0-9]*) - capture group, matches 0-9 none or more times
  • \. matches . literally
  • com matches com literally

One thing to note is that placing the * outside the capture group ([0-9])* will match and pass the if, but will not capture anything within the capture group. This makes sense, but its important to note because you'll get this message:

Notice: Undefined offset: 1 in [...][...] on line 6

for test.com.

If you want to match the number in embed- You can use one of these

     '/test\.com\/embed-([0-9]{3})\.html/'
     '/\/embed-([0-9]{3})\.html/'
     '/\/embed-([0-9]{3})\./'

Depending how specific you want to be. You can play around with different Regx on this page.

https://regex101.com/r/snuqRc/1

Regular expressions are very powerful, they are meant for pattern matching, which is what you need.

Cheers.

Upvotes: 0

A.D.
A.D.

Reputation: 2372

You can use substr_count

if (substr_count($data['url'], 'test.com') > 0) {
    echo "my embed code number 1";
}
else if (substr_count($data['url'], 'test2.com') > 0) {
    echo "my embed code number 2";
}

or strpos

if (strpos($data['url'],'test.com') !== false) {
    echo "my embed code number 1";
}
else if (strpos($data['url'],'test2.com') !== false) {
    echo "my embed code number 2";
}

or preg_match

if(preg_match('/test.com/',$data['url']))
{
    echo "my embed code number 1";
}
else if(preg_match('/test2.com/',$data['url']))
{
    echo "my embed code number 2";
}

Upvotes: 0

chris85
chris85

Reputation: 23892

You can parse the URL to get the host then compare it.

$dataurl = array('http://test.com/embed-990.html', 
                 'http://test2.com/embed-011.html',
                 'http://test3.com/embed-022.html');
foreach($dataurl as $url) {
    switch(parse_url($url, PHP_URL_HOST)) {
        case 'test.com':
            echo 'test domain';
        break;
        case 'test2.com':
            echo 'test domain 2';
        break;
        default:
            echo 'unknown';
        break;
    }
    echo $url . PHP_EOL;
}

Demo: https://3v4l.org/nmukK

For the question Something like SQL “LIKE” you could use a regex in preg_match.

Upvotes: 2

Related Questions