jkrystof Posted March 1, 2009 Share Posted March 1, 2009 If anyone can help out with preg_match_all or some guidance that would be greatly appreciated. I'm trying to pull some user info from my html which looks like below. <div class="td user_ref"> <p class="alignright" id="user"> <span class="Height">FT</span> <span><span class="user_ref" userid="jbk" mwformat=",2" userfield="height1">6.3</span> </span> </p> </div> I am looking to pull only the 6.3 and place it into an array. I've displayed one div class but I will have between 20 to 30 html segments just like this in my complete html. Also, there are other div classes not equal to "td user_ref" within the html which should be ignored. I would like to extract each height and place it within the height array. preg_match_all($string_pattern, $html, $height); I've been attempting to learn the $string_pattern part with regular expressions but only getting constant headaches. all help and thoughts appreciated..thanks Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/ Share on other sites More sharing options...
.josh Posted March 1, 2009 Share Posted March 1, 2009 Assumes userfield="height1" is a unique attribute to the span inside the div you want to match, that will be in all the spans inside those divs. preg_match_all('~<div.+?"td user_ref"[^>]*>.+?<span.+?userfield="height1"[^>]*>(.*?)</span>~is',$html,$height); print_r($height[1]); Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-774033 Share on other sites More sharing options...
jkrystof Posted March 3, 2009 Author Share Posted March 3, 2009 Thanks, that worked for me. There was one issue I was still having on another segment within the html where I'm using regex too. <div class="td user_ref"> <p class="mweight" id="user_id"> Weight Value </p> </div> In this case I used the following regex and I don't know what to put in following the id=??????? because my user id is a numeric value greater than 1, i.e id="user1", "user2"..."user27".."user98" etc. Also I don't want to include the class="mweight" because that is the same in other <p></p> segments I'm not interested in. I can return the first 9 if I use a /d but I don't know how to return for all numbers greater than or equal to 1. preg_match_all('~<div.+?"td user_ref"[^>]*>.+?<p.+?id=???????[^>]*>(.*?)</p>~is',$html,$weight); thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-775763 Share on other sites More sharing options...
.josh Posted March 4, 2009 Share Posted March 4, 2009 id="\d+?" Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-776325 Share on other sites More sharing options...
jkrystof Posted March 5, 2009 Author Share Posted March 5, 2009 thanks, it does work but for some rreason I only get every other user_id entry saved to my $weight array. any thoughts why this would happen? for example, $weight[0]= "user2" $weight[1]= "user4" etc... I'm using the following code preg_match_all('~<div.+?"td user_ref"[^>]*>.+?<p.+?id="user\d+?"[^>]*>(.*?)</p>~is',$html,$weight); the html is the same for each user, <div class="td user_ref"> <p class="mweight" id="user_id"> Weight Value </p> </div> Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-777462 Share on other sites More sharing options...
.josh Posted March 5, 2009 Share Posted March 5, 2009 try adding .*?</div> to the end of your pattern. Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-777503 Share on other sites More sharing options...
jkrystof Posted March 5, 2009 Author Share Posted March 5, 2009 gave it a try but still same result. I'll have to take a deeper look at it later today when I have a bit of time. seems odd to me. thanks Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-777514 Share on other sites More sharing options...
.josh Posted March 5, 2009 Share Posted March 5, 2009 okay then can you give an example of the whole page that you're trying to pull the info from? Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-777557 Share on other sites More sharing options...
jkrystof Posted March 7, 2009 Author Share Posted March 7, 2009 thanks crayon for your help. i played with it some this morning and got it to work now. im not exactly sure why it was skipping every other one but the following worked for me. preg_match_all('~<div.+?class="td.+?"[^>]*>.+?<p.+?id=".+?height.+?"[^>]*>(.*?)</p>~is',$target2,$height); Quote Link to comment https://forums.phpfreaks.com/topic/147451-solved-anyone-able-to-help-on-regex-issue-with-my-html-source-code/#findComment-778958 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.