php - I can't use my regular expression right -
sorry guys, maybe question silly, i'm stuck...again regular expression...
look, have variable, ex:
$str = "[quote]here 2013[/quote]"; $rega = preg_match_all("/[quote](.*)[quote]/i",$str,$hols);
when try $hols variable, this:
quote]here 2013[/quote
but need this:
here 2013
second question when try $str:
$str = "[quote]hello people my...[/quote]";
so, regular expression, like:
quote]hello people
but need:
hello people my...
i'd appreciate if explain me how handle it, how make right. because have no idea how fix it.
in addition frits has said, need escape brackets; asking regex match of characters 'q', 'u', 'o', 't' or 'e', followed anything, followed same characters again; reason match large has fact expression greedy, while frits' suggestion "break" (correctly, though). in short, escape brackets so:
/\[quote\](.*?)\[\/quote\]/mis
more explanation:
considering earlier pattern:
/[quote](.*)[quote]/i
you asking for:
- any character of set [quote]
- any character except newline, 0 or more times
- any character of set [quote]
so, example, string queen's charter!
match, queen's charte
inner match of ueen's chart
(since first , last character class not captured).
if make second step non-greedy, qu
, ee
, , te
, no inner matches @ all.
edit: replying comment
i ran following code:
preg_match_all( '/\[quote\](.*?)\[\/quote\]/mis', "[quote]a test! marvelous test![/quote]", $matches ); var_dump($matches);
and got following result:
array (size=2) 0 => array (size=1) 0 => string '[quote]a test! marvelous test![/quote]' (length=41) 1 => array (size=1) 0 => string 'a test! marvelous test!' (length=26)
Comments
Post a Comment