paulonline2501 Posted January 10, 2012 Share Posted January 10, 2012 hi there, i need a little help with my regex.its been a while since i did this and forgot how to do something quite simple.i want to look for a section of the string that starts in a specific way, has something random in the middle, and ends in a secific way. so for example if i was looking for the the bit that starts 4 and ends 6 in 123456789 i would want to get back 456. the bit in the middle can be any characters and any lenght.i thought i could look for "start"+[.]+"end" but this dfoesnt seem to work. wherethe start is <div class="content">the end is </div> my code so far is below: this ones works if i hard code what im looking for ['stuff to replace']:$pattern = '/\<div class\=\"content\"\>stuff to replace\<\/div\>/';$replacement = '<div class=\"content\">this has been updated</div>';$updatedData = preg_replace($pattern, $replacement, $theFileData);the problem is when i start to look for 'anything' between the start and end:$pattern = '/\<div class\=\"content\"\>+[.]+\<\/div\>/';$replacement = '<div class=\"content\">this has been updated</div>';$updatedData = preg_replace($pattern, $replacement, $theFileData); please help. Link to comment Share on other sites More sharing options...
paulonline2501 Posted January 10, 2012 Author Share Posted January 10, 2012 ...i can get to work for any character but only if there is one character. i need it to look for any character and any number of characters. thats why i put the [brackets] around the. this worked, but only if there is one character:$pattern = '/\<div class\=\"content\"\>.\<\/div\>/'; how do i look for any number of 'any characters'??? Link to comment Share on other sites More sharing options...
paulonline2501 Posted January 10, 2012 Author Share Posted January 10, 2012 i got it working.i put a '*' after the '.' $pattern = '/\<div class\=\"content\"\>.*\<\/div\>/' Link to comment Share on other sites More sharing options...
paulonline2501 Posted January 10, 2012 Author Share Posted January 10, 2012 ...it doesnt work if there are spaces or line returns!how can i include these? i literally what to include anything. i tried adding :space: and :punct: but that didnt work or my punctuation is wrong:$pattern = '/\<div class\=\"content\"\>[.:space::punct:]*\<\/div\>/'; Link to comment Share on other sites More sharing options...
justsomeguy Posted January 10, 2012 Share Posted January 10, 2012 Use the "s" modifier. $pattern = '/\<div class\=\"content\"\>.*\<\/div\>/s'; http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php Link to comment Share on other sites More sharing options...
paulonline2501 Posted January 10, 2012 Author Share Posted January 10, 2012 brillient! thanks old man from the scene. the 's' on the end works perfectly. appreciate the help. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.