jobbe Posted July 4, 2011 Share Posted July 4, 2011 Hi, I have written a function which will replace a string like: {-type:button|info:item|text:Read more about item-} with the following <input type="button" class="button" href="popup.php?r=item" value="Read more about item" /> The problem with this function is that I cannot run this on the entire page, since it would just return the first button. So I need help. I need a function that will check a string of text for the following combination: {- some text -} And then replace that string with the output from the function. It's important that the content between {- and -} can be changed, since the content acts as "variables" for my function. <?PHP function replaceText($str){ $dat = explode('{-',$str); $dat = explode('-}',$dat[1]); $dat = explode('|',$dat[0]); foreach($dat as $id => $str){ $out = explode(':',$str); $new[$out[0]] = $out[1]; } if($new['type']=='button') return '<input type="button" class="button" href="popup.php?r='.$new['info'].'" value="'.$new['text'].'" />'; if($new['type']=='link') { $http = 'http://'; if($new['secure']=='yes') $http = 'https://'; return '<a class="button" href="'.$http.$new['link'].'" target="'.$new['target'].'">'.$new['text'].'</a>'; } } $str_1 = '{-type:button|info:item|text:Read more about item-}'; $str_2 = '{-type:link|target:blank|link:www.google.com|secure:no|text:Read more about item-}'; $str = $str_1.$str_2; echo replaceText($str_1).'<br />'; echo replaceText($str_2).'<br />'; echo replaceText($str).'<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241100-checking-text-for-a-string-to-run-though-function/ Share on other sites More sharing options...
Psycho Posted July 5, 2011 Share Posted July 5, 2011 make the function recursive where it searches for text matching the pattern, replaces it and then calls the function again until all matches have been found/replaced. I made a lot of changes to your function to remove inefficiencies. function replaceText($str) { //Search the string for a {} match if(preg_match("#{-[^-]+-}#", $str, $matchAry)==0) { //If no matches found return the string return $str; } //Parse the match found $matchStr = $matchAry[0]; $data = array(); foreach(explode('|', substr($matchStr, 2, -2)) as $pair) { list($name, $value) = explode(':', $pair); $data[$name] = $value; } //Determine replacement string for match found switch($data['type']) { case 'button': $replaceStr = "<input type=\"button\" class=\"button\" href=\"popup.php?r={$data['info']}\" value=\"{$data['text']}\" />"; break; case 'link': $http = ($data['secure']=='yes') ? 'https://' : 'http://'; $replaceStr = "<a class=\"button\" href=\"{$http}{$data['link']}\" target=\"{$data['target']}\">{$data['text']}</a>"; break; default: //Need a default to prevent infinite loops $replaceStr = ''; } //Repalce the matched {} string witht he replacement defined $str = str_replace($matchStr, $replaceStr, $str); //Call function recursively until all {} matches are replaces return replaceText($str); } $str_1 = '{-type:button|info:item|text:Read more about item-}'; $str_2 = '{-type:link|target:blank|link:www.google.com|secure:no|text:Read more about item-}'; $str = $str_1.$str_2; echo replaceText($str); Output: <input type="button" class="button" href="popup.php?r=item" value="Read more about item" /> <a class="button" href="http://www.google.com" target="blank">Read more about item</a> Quote Link to comment https://forums.phpfreaks.com/topic/241100-checking-text-for-a-string-to-run-though-function/#findComment-1238391 Share on other sites More sharing options...
jobbe Posted July 5, 2011 Author Share Posted July 5, 2011 Thanks mjdamato - Works Quote Link to comment https://forums.phpfreaks.com/topic/241100-checking-text-for-a-string-to-run-though-function/#findComment-1238398 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.