Jump to content

Help with PHP's PCRE Patterns


marcbraulio

Recommended Posts

Hey everyone!

New member here still getting the hang of PHP. Any help is much appreciated.

 

Suppose the array contains:

array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php');

 

How do I get php to find the array item that contains let's say "css" and instead of returning just "css" it returns the entire string that has the "css" in it, instance "http://www.php.net/styles/site.css".

 

I was thinking of using the following:

preg_match('/[sOME_PCRE_PATTERN].css/', 'http://static.php.net/www.php.net/styles/site.css', $matches);

 

Problem is since the links could be different and have all sorts of different characters in it, I have no idea what to put for the PCRE_PATTERN.

 

In other words, this:

$links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php');

foreach ($links as $key) {
 preg_match('/css/', $key, $matches);
 $results[] = $matches;
}
print_r ($results);

 

Returns this:

 Array ( [0] => Array ( [0] => css ) [1] => Array ( ) [2] => Array ( ) )

 

I would like it to return:

 Array ( [0] => Array ( [0] => http://www.php.net/styles/site.css ) [1] => Array ( ) [2] => Array ( ) )

 

 

I hope that makes sense to you guys. Once again, any help is appreciated and thank you!

Link to comment
Share on other sites

$links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php');
$results = array();

foreach($links AS $link){
    if(strpos($link, '.css') !== false){
        $results[] = $link;
    }
}

Link to comment
Share on other sites

For something simple like that there's no need to use regular expressions.

 

Loop through the array, using strpos to check if the value exists. If it does, 'push' the value into a $results array.

 

Genius! Thank you!  :)

 

If anyone else is curious, this is the final working solution:

 

Original array:

$links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php');

Solution suggested by "xyph"

foreach ($links as $key) 
{
 if (strpos($key,'css'))
 {
 	$results[] = $key;
 }
}

Final Result:

Array ( [0] => http://www.php.net/styles/site.css )

 

 

EDIT: Or use the solution above by "mgallforever" which I somehow didn't see until now.

Link to comment
Share on other sites

$links = array ('http://www.php.net/styles/site.css', 'http://www.example.com/styles/some.js', 'http://www.domain.net/styles/hey.php');
$results = array();

foreach($links AS $link){
    if(strpos($link, '.css') !== false){
        $results[] = $link;
    }
}

 

Thank you very much =]

Out of curiosity, is it considered a good practice to declare the

$results = array();

before hand?

Link to comment
Share on other sites

Yes, it's better practise.

 

If $results is undefined, and you use $results[]... no error will be thrown.

If $results is a defined non-array, and you use $results[]... an error will be thrown, possibly fatal.

 

You need to use if( strpos(args) !== FALSE ). If 'css' are the first characters in your string, the 'offset' strpos returns would be 0, which translates to FALSE unless you use a STRICT (!==) comparison. It's explained in more detail in the manual.

Link to comment
Share on other sites

Yes, it's better practise.

 

If $results is undefined, and you use $results[]... no error will be thrown.

If $results is a defined non-array, and you use $results[]... an error will be thrown, possibly fatal.

 

You need to use if( strpos(args) !== FALSE ). If 'css' are the first characters in your string, the 'offset' strpos returns would be 0, which translates to FALSE unless you use a STRICT (!==) comparison. It's explained in more detail in the manual.

 

Got it! =]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.