I have this code as input. <div name="Planning Reviews"><h2>Planning - Reviews</h2> <ul> <li><a href="http://example.edu/templates/sa-ath.rtf">saa-ath.rtf</a></li> </ul> </div> <div name="Design Reviews"><h2>Design - Reviews</h2> <ul> <li>No templates are available for this Task</li> </ul> </div> <div name="Planning IP"> <h2>Planning - IP Waiver</h2> <ul> <li><a onmousemove="prevHover(this)" href="http://example.edu/templates/wq9-26-07.rtf">asd 9-26-07.rtf</a></li> <li><a onmousemove="prevHover(this)" href="http://example.edu/templates/qwe_2 party_072005.rtf">qw1_2 party_072005.rtf</a></li> </ul> </div> PHP: Now I am trying to use Regular Expression to find all div elmts that have a name that starts with the first word. For example, if I give the name "Planning" I want it to find all div elmts that have a name that starts with "Planning". preg_match_all("/<div name=\"{$wantedID}\">([^`]*?)<\/div>/", $file, $matches); PHP: The above expression works only if the 'name' matches exactly. I need help being modifying expression to find if 'name' starts with that word but can be anything after that to get the first and last div from above.
preg_match_all("/<div name=\"{$wantedID}[A-Za-z0-9_ ]+\">([^`]*?)<\/div>/", $file, $matches); PHP: This should match all the latin characters, numbers, dashes, underscores. If you need more characters, then add them. + sign at the end means that there are more that one characters. If you know, that you are looking for 3 to 6 characters after your $wantedId, then you would use: [A-Za-z0-9_ ]{3,6}
I tried that but $matches still spits out all the div regardless if name is 'Planning' or anything else
Try again, because testing it in the tool http://gskinner.com/RegExr/ with the following regex works: <div name="Planning[A-Za-z0-9_ ]+">([^`]*?)<\/div>
Tried it again, this time it worked. Thanks so much! that tool is pretty helpful too, it just took me a little time to figure out how it works