Matei Zoc
Matei Zoc

Reputation: 3949

Preg_match get the value

How can get the exact way the value of a preg_match example of what i'm trying to do

website url: www.website.com/go/from-youtube-com ( this is show me just the videos added from youtube )

   $pattern = '/from-(.*?)-com/is';
   if(preg_match( $pattern, $search_query, $ar)) {
    $webvideos = $ar[1] . '.com';
    $webstatus = 1;
   }

This command is working but i test and if i make the url: www.website.com/go/asdasd-from-youtube-com or www.website.com/go/asdasd-from-youtube-com-ewrwer is still show me the videos from the youtube but normally was need to show false ...

Edit:

$search_query = 'from-youtube-com'; // (good url)
$search_query = 'dasdasd-from-youtube-com'; // (bad url)
$search_query = 'dasdasd-from-youtube-com-dasdsa'; // (bad url)

Upvotes: 0

Views: 223

Answers (2)

maxhb
maxhb

Reputation: 8865

As @mmm already pointed out you have to treat the / character in a special way.

a) Use another delimiter for your regex like

$pattern = '#/from-(.*?)-com/#is'; // use # instead of / delimiter

or b) escape the / like in

$pattern = '/\/from-(.*?)-com\//is'; // escape/ by using \/

Upvotes: 1

mmm
mmm

Reputation: 1151

the "/" in your regex is the delimiter
if you want to use the slash in the regex, you have to choose another delimiter like "!" :

$pattern = '!/from-(.*?)-com/!is';

Upvotes: 2

Related Questions