Skip to Content Skip to Menu

🌟 CB Editor Assistant 1.0.0 is here! Discover our new AI Joomla Plugin that wrote its story! (and this banner!)
Start at just $12.50/month* or 💸 save 30% with our 🛍️ Black Friday Intro Offer for your subscription's lifetime
🎉 Black Friday sale is here! Great savings on professional and developer memberships! Get 25% off now with code BLACK-FRIDAY-2024!

CB Replacer Bot - syntax

  • micheleq
  • micheleq
  • OFFLINE
  • Posts: 144
  • Thanks: 22
  • Karma: 6
9 months 1 week ago #337239 by micheleq
CB Replacer Bot - syntax was created by micheleq
Hello,
I'm having trouble figuring out the syntax for my replacement. 
The goal is to insert Wikipedia links at the bottom of articles. I've been given a spreadsheet with different entries for each article (hundreds, close to 1000 entries.)
My idea is to enter them as an expression to be replaced e.g. 
Code:
{wp = The Beatles, Liverpool, John Lennon, Paul McCartney, George Harrison, Ringo Starr}
And generate the output:
Code:
<ul> <li><a href="https://en.wikipedia.org/wiki/The Beatles" target="_blank">The Beatles</a></li> <li><a href="https://en.wikipedia.org/wiki/Liverpool" target="_blank">Liverpool</a></li> <li><a href="https://en.wikipedia.org/wiki/John Lennon" target="_blank">John Lennon</a></li> <li><a href="https://en.wikipedia.org/wiki/Paul McCartney" target="_blank">Paul McCartney</a></li> <li><a href="https://en.wikipedia.org/wiki/George Harrison" target="_blank">George Harrison</a></li> <li><a href="https://en.wikipedia.org/wiki/Ringo Starr" target="_blank">Ringo Starr</a></li> </ul>

From the spreadsheet, I can enter them through a query to the appropriate article, that's not a problem. 
I'm failing on creating the replacer code. 
I'm using PHP to split the expressions using the commas (,) and create the output, which works fine as PHP but I can't get it to work in the replacer
In the 'From' box I've tried (with REGEXP and Code (PHP) on) :
Code:
/\{wp =(.+)\}/
And the PHP code that works is:
Code:
<?php $keywords = preg_split("/[,]+/", $1); $textoutput = "<ul>"; foreach ($keywords as $link) { $link = ltrim($link); $textoutput .= "<li><a href='https://en.wikipedia.org/wiki/" . $link . "' target=_blank >" . $link . "</a></li>"; } $textoutput .= "</ul>"; echo $textoutput; ?>

How can I get this to work? 
Thank you! 
Tomás

Please Log in or Create an account to join the conversation.

  • micheleq
  • micheleq
  • OFFLINE
  • Posts: 144
  • Thanks: 22
  • Karma: 6
9 months 1 week ago #337241 by micheleq
Replied by micheleq on topic CB Replacer Bot - syntax
As an update, this is where I'm stuck.
I have in the "From" box
Code:
/\{wp =(.+)\}/
In the "To" box
Code:
$wiki = '$1'; $textoutput = "<ul>"; $keywords = preg_split("/[,]+/",  $wiki); foreach ($keywords as $link) {      $link = ltrim($link);      $textoutput .= "<li><a href='https://en.wikipedia.org/wiki/" . $link . "' target=_blank >" . $link . "</a></li>"; } $textoutput .= "</ul>"; echo $textoutput;
My output is:
Code:
<ul> <li><a href="https://en.wikipedia.org/wiki/ The Beatles, Liverpool, John Lennon, Paul McCartney, George Harrison, Ringo Starr" target="_blank"> The Beatles, Liverpool, John Lennon, Paul McCartney, George Harrison, Ringo Starr</a></li> </ul>

But if I replace
Code:
$wiki = '$1';
 
with 
Code:
$wiki = ' The Beatles, Liverpool, John Lennon, Paul McCartney, George Harrison, Ringo Starr';
 
Then I do get what I'm looking for, which is: 
Code:
<ul> <li><a href="https://en.wikipedia.org/wiki/The Beatles" target="_blank">The Beatles</a></li> <li><a href="https://en.wikipedia.org/wiki/Liverpool" target="_blank">Liverpool</a></li> <li><a href="https://en.wikipedia.org/wiki/John Lennon" target="_blank">John Lennon</a></li> <li><a href="https://en.wikipedia.org/wiki/Paul McCartney" target="_blank">Paul McCartney</a></li> <li><a href="https://en.wikipedia.org/wiki/George Harrison" target="_blank">George Harrison</a></li> <li><a href="https://en.wikipedia.org/wiki/Ringo Starr" target="_blank">Ringo Starr</a></li> </ul>

It seems I can't process the string to an array from the regexp string ($1) but if I use that exact text (forced) as the string, it delivers fine. 

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48560
  • Thanks: 8292
  • Karma: 1445
9 months 1 week ago #337245 by krileon
Replied by krileon on topic CB Replacer Bot - syntax
CB Replacer Bot is just using preg_replace when "Is REGEXP" is set to "Yes". That's basically it. From is the $pattern, To is the $replacement, and the article is the $subject. So for that usage you'd need to keep that in mind.

When "Process "To" Code (PHP)" is set to "Yes" to runs "To" through PHP processing and expects it to be PHP. This is done before it does the replacement. So if "Is REGEXP" is "Yes" then the result of your PHP is passed as the $replacement for the preg_replace.

Your PHP code has access to the following variables: $context, $content, $input

$content is the the article object in the case of using this inside of a Joomla article. To get the article contents you'd use $content->text and perform whatever PHP you need on it. In short "From" just becomes a matcher and your PHP is entirely responsible for parsing the text and returning whatever you're wanting it to return.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: micheleq

Please Log in or Create an account to join the conversation.

  • micheleq
  • micheleq
  • OFFLINE
  • Posts: 144
  • Thanks: 22
  • Karma: 6
9 months 1 week ago #337262 by micheleq
Replied by micheleq on topic CB Replacer Bot - syntax
Hi Kyle,
Thanks for the detailed clarification. I understand now that the PHP code is processed BEFORE the replacement. That was why I couldn't get it to work. 
I got it to work with PHP getting the article text ($content->text), processing it and then the replacement worked perfectly. 
Thank you, 
Tomás 
The following user(s) said Thank You: krileon

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum