tomindo Posted February 25, 2011 Share Posted February 25, 2011 $html = "<p>test</p> <p>test1</p> <p>test2</p>"; my expected result is array(0=>"<p>test</p>",1=>"<p>test1</p>",2=>"<p>test2</p>") what is regular expression for it ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/228771-regular-express-for-tag/ Share on other sites More sharing options...
.josh Posted February 25, 2011 Share Posted February 25, 2011 preg_match_all('~<p>.*?</p>~',$html,$ptags); $ptags = $ptags[0]; example: $html = "<p>test</p> <p>test1</p> <p>test2</p>"; preg_match_all('~<p>.*?</p>~',$html,$ptags); $ptags = $ptags[0]; echo "<pre>"; print_r($ptags); // output: Array ( [0] => <p>test</p> [1] => <p>test1</p> [2] => <p>test2</p> ) Quote Link to comment https://forums.phpfreaks.com/topic/228771-regular-express-for-tag/#findComment-1179530 Share on other sites More sharing options...
tomindo Posted February 25, 2011 Author Share Posted February 25, 2011 great ,thanks I never used the tildle. Quote Link to comment https://forums.phpfreaks.com/topic/228771-regular-express-for-tag/#findComment-1179668 Share on other sites More sharing options...
.josh Posted February 25, 2011 Share Posted February 25, 2011 All php regex patters have to have a delimiter wrapped around the pattern. The tilde is just being used as the pattern delimiter. Other than that, it has no special significance. You can use most any non-alphanumeric character as the pattern delimiter, as long as you make sure to escape instances of it within your pattern. I use the tilde because it rarely ever comes up in stuff you are trying to match. Especially when making a pattern that matches html code. Lots of people use /.../ well in html code there are a lot of forward slashes, so you have to escape it every time you use it in your pattern. Quote Link to comment https://forums.phpfreaks.com/topic/228771-regular-express-for-tag/#findComment-1179733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.