glenelkins Posted October 8, 2009 Share Posted October 8, 2009 Hi Lets say i have the following string Before{block}After and the following preg_match $content = "Before{block}After"; preg_match ( '%([A-Za-z0-9]*){(.*)}([A-Za-z0-9]*)%', stripslashes ( $content ), $data ); Works perfect, picks out $data[1] = "Before" $data[2] = "block" $data[3] = "After" But, say i have this Before{block}After{block} it doesnt work, it doesnt seem to pick up that i need to have $data[1] = "Before" $data[2] = "block" $data[3] = "After" $data[4] = "block" Any help would be great Quote Link to comment Share on other sites More sharing options...
salathe Posted October 8, 2009 Share Posted October 8, 2009 Depending what exactly you need, there might well be several different "answers" to your problem. Do you literally just want to amend the regular expression to accept that second {block}, or any number of {block}s with before/after text, or ... ? Quote Link to comment Share on other sites More sharing options...
.josh Posted October 8, 2009 Share Posted October 8, 2009 Are you looking to just grab everything that is not within {block} ? Quote Link to comment Share on other sites More sharing options...
glenelkins Posted October 8, 2009 Author Share Posted October 8, 2009 hi i am wanting to grab everything outside the {block} and the {block} itself. so for example the actual thing may look like this Some text {block identifier='test'} some more text {block identifier='another_test'} and so on. My code is basically used so i can take the {block} tags and read the identifier and load up a plugin in my php based on what the identifier is set to, perform the code operation and replace the whole {block} tag... But for now, im trying to get it to identify any number of block tags Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 8, 2009 Share Posted October 8, 2009 <?php $content = "Before{block}After{blockb}Now{Blockc}A"; preg_match_all('~([a-zA-Z0-9]+)(?:{(.+?)})?~', stripslashes ($content), $tdata ); $data = array(); foreach($tdata[2] as $k=>$v) { $data[] = $tdata[1][$k]; if($v != "") { $data[] = $v; } } print_r($data); ?> Quote Link to comment Share on other sites More sharing options...
fooDigi Posted October 8, 2009 Share Posted October 8, 2009 if your goal is to figure out what identifiers are used... this will return an array of identifiers stored in $matches[1]... <? $str = "before text{block identifier='test'}middle text{block identifier='test2'}end text"; preg_match_all("/{block identifier='([^']+)'}/",$str,$matches); print_r($matches[1]); ?> Quote Link to comment Share on other sites More sharing options...
glenelkins Posted October 8, 2009 Author Share Posted October 8, 2009 fooDigi Even with all the years programming i have done, i still have a big issue with getting to grips with regex. Could you possibly break down your example and tell me what its doing. the [^'] part i understand, butut what is the + for? Quote Link to comment Share on other sites More sharing options...
fooDigi Posted October 8, 2009 Share Posted October 8, 2009 sure. the plus char will basically match one or more instances of the previous expression... basically will match one or more characters that are not single quotes... Quote Link to comment Share on other sites More sharing options...
fooDigi Posted October 8, 2009 Share Posted October 8, 2009 also, since you will want to replace the entire {block}, $matches[0] will contain those matched strings... so from there you will be able to replace those with the output of your plugins... forgive me, but i went a bit further with my code, and i might as well post it... ignore it, if it is a total miss on what you need... <? function loadPlugin($id) { switch($id) { case 'test': return '[plugin test loaded]'; break; case 'test2': return '[plugin test2 loaded]'; break; case 'test3': return '[plugin test3 loaded]'; break; case 'test4': return '[plugin test4 loaded]'; break; default: return '[invalid plugin]'; } } $str = "before text{block identifier='test'}middle text{block identifier='test2'}end {block identifier='test3'}text{block identifier='test4'}sdfasd fasdf asdf"; preg_match_all("/{block identifier='([^']+)'}/",$str,$matches); for($i=0;$i<count($matches[0]);$i++) { $text = loadPlugin($matches[1][$i]); $str = str_replace($matches[0][$i],$text,$str); } echo $str; ?> Quote Link to comment Share on other sites More sharing options...
glenelkins Posted October 8, 2009 Author Share Posted October 8, 2009 hi well i thought .* matches any character any number of times? or are you saying + matches between [] Quote Link to comment Share on other sites More sharing options...
Garethp Posted October 8, 2009 Share Posted October 8, 2009 Okay, + is the same as * except * will match it 0 times. So say you have {} then {(.*)} will match it, but + matches once or more so {(.+)} will not, because there has to be at least one thing there. Recap: * Matches 0 or more + Matches 1 or more Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted October 8, 2009 Share Posted October 8, 2009 glenelkins, you can read more about regex in general in the following links: http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax http://www.regular-expressions.info/ http://weblogtoolscollection.com/regex/regex.php These should be more than enough to get things started.. then, if you really want to get on the ball; http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239475391&sr=8-1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.