Page 1 of 1

Regexp problem.

Posted: Sat Nov 15, 2008 6:54 am
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?

Re: Regexp problem.

Posted: Sat Nov 15, 2008 8:08 am
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);

Re: Regexp problem.

Posted: Sat Nov 15, 2008 8:22 am
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+)(?=\

Re: Regexp problem.

Posted: Sat Nov 15, 2008 8:28 am
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);