Jump to content

Checking text for a string to run though function


jobbe

Recommended Posts

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 />';
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.