Regexp problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TeNDoLLA
Forum Newbie
Posts: 8
Joined: Wed Nov 12, 2008 3:52 pm

Regexp problem.

Post by TeNDoLLA »

Hi,
So I have a string in php that holds html code:

Code: Select all

 
$string = '
<head>
<title>test</title>
</head>
        <body>
                <table border="1" cellpadding="2" cellspacing="2">
                <tr>
                <td valign="top">
                        [some_stuff]
                        <a href="#>jottain</a>
                        [some_stuff_ends]
                </td>
                </tr>
                </table>
        </body>
</html>';
 

And I try to get the data between [some_stuff] and [some_stuff_ends] with regexp without success. I have tried with this and it does not work.

Code: Select all

 
preg_match('/\[some_stuff\](.+)\[some_stuff_ends\]/',$string,$matches); 
 
Has this something to do with line breaks or something else maybe? Anyone got solution?
waqas_punjabian
Forum Commoner
Posts: 67
Joined: Wed Aug 10, 2005 9:53 am

Re: Regexp problem.

Post by waqas_punjabian »

this is because of the white spaces in html. If you remove those white spaces this RegEx will work fine.
use following code before the preg_match function.

Code: Select all

preg_replace( "/(?:(?<=\>)|(?<=\/\))(\s+)(?=\<\/?)/","", $string ); 
preg_match('/\[some_stuff\](.+)\[some_stuff_ends\]/',$string,$matches);
TeNDoLLA
Forum Newbie
Posts: 8
Joined: Wed Nov 12, 2008 3:52 pm

Re: Regexp problem.

Post by TeNDoLLA »

Your regular expression gives me warning:
Warning: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 34 in \test\temp.php on line 30 and line 30 is this

Code: Select all

 
preg_replace("/(?:(?<=\>)|(?<=\/\))(\s+)(?=\
TeNDoLLA
Forum Newbie
Posts: 8
Joined: Wed Nov 12, 2008 3:52 pm

Re: Regexp problem.

Post by TeNDoLLA »

Problem solved, this seems to do the trick:

Code: Select all

 
$pattern='/\[some_stuff\](.*)\[some_stuff_ends\]/s';
preg_match_all($pattern, $string, $matches);
 
Post Reply