ldb358 Posted October 2, 2009 Share Posted October 2, 2009 i've been looking for a way to do this but cant seem to find anything: what i want to do is say i have {test} i want to be able to replace it with 'hello' but if i have {test(1)} i want to replace test with the first value in an array(i already have a function that reads and replaces all of {test} but i cant get the array/number part of it to work). Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/ Share on other sites More sharing options...
PugJr Posted October 2, 2009 Share Posted October 2, 2009 Could you tell me exactly what you want to turn into what? So are you wanting to remove the "}" and "{" from the end parts? Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928888 Share on other sites More sharing options...
ldb358 Posted October 2, 2009 Author Share Posted October 2, 2009 okay what im doing is a template parsing system where it reads the file and would replace {test} with content but lets say i have a place where it lists out 20 videos i want to be able to put {test(1)} and it would be replaced with the first video(which would be stored in an array) and {test(2)} to be replaced with the second video and so on Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928891 Share on other sites More sharing options...
TeNDoLLA Posted October 2, 2009 Share Posted October 2, 2009 Would this work? <?php $str = 'some text and here is {test(1)} and here is another {test(2)} and it continues..'; function ownReplace($matches) { $replaces = array('first', 'second', 'third'); return $replaces[$matches[1]]; } $str = preg_replace_callback('/\{.*?\(([0-9]+)\)\}/', 'ownReplace', $str); // Replace with number from array by index. var_dump($str); Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928895 Share on other sites More sharing options...
ldb358 Posted October 2, 2009 Author Share Posted October 2, 2009 it works perfectly now that i took a second look but how would i do it if i say had like {test(1)} but also had {hello(1)} and i didn't want hello switched(at least with that function) thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928899 Share on other sites More sharing options...
TeNDoLLA Posted October 2, 2009 Share Posted October 2, 2009 Yes but you can try it can't you? The piece I posted replaces the {test(1)} with the value in $replaces[1] and {test(2)} with the value $replaces[2]. This will replace the values based on the array index. So you have to just make sure that your first video has then index 1 in the video array. Also if using with templates they are probably multi line data so you might wanna add 's' -modifier in the end of the regular expression so it will ignore new lines. <?php $str = preg_replace_callback('/\{.*?\(([0-9]+)\)\}/s', 'ownReplace', $str); // Replace with number from array by index. Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928900 Share on other sites More sharing options...
ldb358 Posted October 2, 2009 Author Share Posted October 2, 2009 okay thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928901 Share on other sites More sharing options...
RussellReal Posted October 2, 2009 Share Posted October 2, 2009 r u interested in using regex.. or you want to do this solely in php (well you know what I mean..) regex would be easier but with plain old php you'd do <?php $data = "hello {abc(0)}"; $abc = array("world"); $start = 0; while ($start = strpos($data,"{",$start)) { $end = strpos($data,"}",++$start); $in = substr($data,$start,$end - $start); if ($ns = strpos($in,"(")) { $var = substr($in,0,$ns); $index = substr($in,++$ns,strpos($in,")") - $ns); $repval = ${$var}[$index]; } else { $repval = ${$in}; } $data = str_replace('{'.$in.'}',$repval,$data); $start = strlen($repval) + $start; } echo $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928902 Share on other sites More sharing options...
TeNDoLLA Posted October 2, 2009 Share Posted October 2, 2009 it works perfectly now that i took a second look but how would i do it if i say had like {test(1)} but also had {hello(1)} and i didn't want hello switched(at least with that function) thanks for the help So you want only tags with certain name to be replaced? <?php $str = 'some text and here is {test(1)} and here is another {test(2)} and it continues.. {hello(1)} ... {somemore(10)}'; // And change the regexp to this.. will match all tags having 'test' $str = preg_replace_callback('/\{test\(([0-9]+)\)\}/', 'ownReplace', $str); // Replace with number from array by index. You can probably easily spot the word test in the regular expression. Just change that whatever you wish. Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928903 Share on other sites More sharing options...
TeNDoLLA Posted October 2, 2009 Share Posted October 2, 2009 r u interested in using regex.. or you want to do this solely in php (well you know what I mean..) regex would be easier but with plain old php you'd do <?php $data = "hello {abc(0)}"; $abc = array("world"); $start = 0; while ($start = strpos($data,"{",$start)) { $end = strpos($data,"}",++$start); $in = substr($data,$start,$end - $start); if ($ns = strpos($in,"(")) { $var = substr($in,0,$ns); $index = substr($in,++$ns,strpos($in,")") - $ns); $repval = ${$var}[$index]; } else { $repval = ${$in}; } $data = str_replace('{'.$in.'}',$repval,$data); $start = strlen($repval) + $start; } echo $data; ?> This gives me warning with your test data and when I tested adding more tags with different names this will produce even more notices (error reportin E_ALL). Not that this will no prevent the script from running because they are not real errors but it is always good to program it that way there won't be any warnings or notices. Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928905 Share on other sites More sharing options...
RussellReal Posted October 2, 2009 Share Posted October 2, 2009 okay.. its a warning which means the loop is gonna terminate because the offset no-longer exists.. <?php error_reporting(E_ALL); $data = "hello {abc(0)}, {ab} and like it!"; $abc = array("worl{d}"); $ab = "eat the banana!!"; $start = 0; while ($start = @strpos($data,"{",$start)) { $end = strpos($data,"}",++$start); $in = substr($data,$start,$end - $start); if ($ns = strpos($in,"(")) { $var = substr($in,0,$ns); $index = substr($in,++$ns,strpos($in,")") - $ns); $repval = ${$var}[$index]; } else { $repval = ${$in}; } $data = str_replace('{'.$in.'}',$repval,$data); $start = strlen($repval) + $start; } echo $data; ?> just throw in the @ to suppress warnings and you're all set Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-928909 Share on other sites More sharing options...
ldb358 Posted October 3, 2009 Author Share Posted October 3, 2009 ive gotten it down to this to search and replace through the document: foreach($this->filter as $name => $value){ $this->output = preg_replace('/{$name.*?([0-19]).*?}/', $value, $this->template); } but it doesn't actually replace any thing Ive never worked with regex so i dont exactly know what I'm doing but any help would be nice Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-929532 Share on other sites More sharing options...
RussellReal Posted October 3, 2009 Share Posted October 3, 2009 excuse me.. this is NOT a regex question my answer does everything he needed it to do, hes just overlooking mine lol Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-929539 Share on other sites More sharing options...
ldb358 Posted October 3, 2009 Author Share Posted October 3, 2009 on yours then i can't get to work exactly how i need it, all my tags are passed to the function via array these are the only ones i want changed and the values are passed in the same array for example $array['test'] = 'hello' and i need it to replace the tag(key) with the value for it Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-929742 Share on other sites More sharing options...
RussellReal Posted October 3, 2009 Share Posted October 3, 2009 mine replaces {abc(0)} with $abc[0] and {wfg} with $wfg.. whatever variables you have in the string have those variables in php variables, and it'll work just fine. theres no reason this post should have 14 posts (considering mine) and regex will not give you any advantage over plain old php, more than likely will just slow your script down as it goes back and forward and never just left to right Quote Link to comment https://forums.phpfreaks.com/topic/176257-solved-getting-the-value-in-between-2-characters/#findComment-929746 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.