Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-09-16 07:47:02

Myusername
Member
Registered: 2019-12-12
Posts: 165

str_replace and preg_replace shortcode

When I started at Textpattern I used a lot of the rah_replace and pax_grep plugins, as I suck at PHP it helped me a lot. But that was before I got to know the <txp:yield/> tag, some time ago I compiled both plugins in one form, and have been using them ever since.

<txp:variable name="type" value="str_replace"/>

<txp:if_yield name="type" value="preg">
    <txp:variable name="type" value="preg_replace"/>
</txp:if_yield>

<txp:variable name="toReplace" trim>
   <txp:yield name="content" default='<txp:yield />' />
</txp:variable>

<txp:php>
    $from = parse('<txp:yield name="from"/>');
    $to = parse('<txp:yield name="to"/>');    
    $type = parse('<txp:variable name="type"/>');
    $content = parse('<txp:variable name="toReplace"/>');
    $delimiter = parse('<txp:yield name="delimiter" default=","/>');
    $from = explode($delimiter,$from);
    $to = explode($delimiter,$to);
    $to_count = count($to);
    if($to_count === 1){
        $to = implode("",$to);
    }
    echo $type($from, $to, $content);
</txp:php>

Use like this:

<txp::replace from="quick" to="slow" content="The quick brown fox jumped over the lazy dog"/>

or

<txp::replace from="quick,lazy" to="slow" content="The quick brown fox jumped over the lazy dog"/>

or

<txp::replace from="quick,lazy" to="slow,any" content="The quick brown fox jumped over the lazy dog"/>

By default, str_replace is used, but you can use preg_replace if you want, just use the attribute type="preg"

That’s it, I did it the way I know it, if there’s something wrong tell me so I can learn and update.

Edit:

I updated the shortcode to follow Bloke recommendation: Now you can use it as a container. I also added the delimiter attribute, so if you need to use commas in your replacements, the shortcode does not become useless:

<txp::replace delimiter="|" from="quick|lazy" to="slow|tired">The quick brown fox jumped over the lazy dog</txp::replace>

Last edited by Myusername (2021-07-06 13:04:53)

Offline

#2 2020-09-16 08:08:12

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,446
Website GitHub

Re: str_replace and preg_replace shortcode

Very handy! One thing you might like to do is tweak the content slightly to allow for more complex content scenarios. If you adjust the code as follows then people can either choose to supply the content attribute or use the tag as a container (the bare <txp:yield /> as default):

<txp:variable name='type' value='str_replace'/>

<txp:if_yield name='type' value='preg'>
    <txp:variable name='type' value='preg_replace'/>
</txp:if_yield>

<txp:variable name="toReplace" trim>
   <txp:yield name="content" default='<txp:yield />' />
</txp:variable>

<txp:php>
    $from = parse('<txp:yield name="from"/>');
    $to = parse('<txp:yield name="to"/>');    
    $type = parse('<txp:variable name="type"/>');
    $content = parse('<txp:variable name="toReplace"/>');
    $from = explode(",",$from);
    $to = explode(",",$to);
    $to_count = count($to);
    if($to_count === 1){
        $to = implode("",$to);
    }
    echo $type($from, $to, $content);
</txp:php>

So you could then use:

<txp::replace from="quick,lazy" to="slow,tired">The quick brown fox jumped over the lazy dog</txp::replace>

This just makes it easier if you’re embedding other tags in your content, so it reduces the need to worry about proper attribute escaping and quoting.

Thanks for sharing this really helpful shortcode.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2020-09-16 08:14:16

etc
Developer
Registered: 2010-11-11
Posts: 5,188
Website GitHub

Re: str_replace and preg_replace shortcode

You should check $type value, it’s risky to allow any php function to be called by users.

Edit: sorry, bad read.

Actually, preg_replace case should be handleable via global trim and replace attributes, not for arrays though.

Yep, handy, thanks for sharing.

Offline

#4 2020-09-16 18:02:28

Myusername
Member
Registered: 2019-12-12
Posts: 165

Re: str_replace and preg_replace shortcode

Bloke wrote #325906:

If you adjust the code as follows then people can either choose to supply the content attribute or use the tag as a container (the bare <txp:yield /> as default):

Perfect. It was my initial idea, but I didn’t quite understand how to use it as a container. Does the empty <txp:yield/> tag return the contents of the container? What inattention of mine. Great to know that!

Thanks for improving the shortcode.

Offline

Board footer

Powered by FluxBB