Sign in to follow this  
Supaflyfrank

[web] Features of preg_match And ereg

Recommended Posts

Ok I have a question how can I get the features of preg_match with the optional PREG_PATTERN_ORDER gives the offset where the match begins and then ereg can return the number of characters match. Is there a way I can combine both of that? Is there another function that I am missing that does this?

Share this post


Link to post
Share on other sites
No, but you can workaround it.

eg. Surround your entire experssion with parenthesis. So each element in $matches (except for the first) will contain the entire matched string.... and you will have several such elements in the array depending on how many sets you have matched.

Then just loop through the array items from $matches[1] to the end, counting the length in each of them... sum it up and you have the total number of matched characters!

Share this post


Link to post
Share on other sites
But what I found out that the matched string in the array isnt the same as the string but instead of which part of the pattern is matched. Like if I wanted to match /< *hey>/
and the string you provide is <     hey>
the string that will be returned is < hey>

Share this post


Link to post
Share on other sites
If you're using ereg, the function should return true or false.

If you're useing preg_match, it should return the number of times a pattern occures.

Though, I'm not 100% sure what you're trying to do. There are tons of preg and ereg and x_replace functions in PHP to do just about anything you need it to.

Share this post


Link to post
Share on other sites
I'm trying to retrieve the css inline style from a webpage. They problem that I am having is that they are so many ways to have a <style> <style type = "text/css"> <style type = 'text/css'> etc... A regular search wont work since an extra space will cause the search not be matched. Only way I can find to fix this is to use pattern expression but the problem is I need to grab everything within <style> </style> And remove it from the html file and store it in another variable.

Share this post


Link to post
Share on other sites
I think this may get you a little closer to solving your problem. Here's a function that will take all the style data and returns it as an array (in case there's more than one style tag in the html)



function get_styles($str)
{
$pat = "#<style([^<>]*)>(.+?)</style>#is";
preg_match_all($pat, $str, $matches);
return $matches[2];
}




If you wish to actually remove styles also, you can use the same pattern in preg_replace...



$str = preg_replace("#<style([^<>]*)>(.+?)</style>#is", "", $str);




Hope this helps...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this