I'm writing a plugin for a CMS (Joomla) to extract part numbers from the Joomla Articles and build a string of static HTML wrapped around it with the exact part number in it.
The entire article is stored in $article->text.
These are the steps:
Note: In this example, we would be ignoring the category field of "Flux Capacitor" since only the part number needs to be extracted to build the static HTML replacement. The category field would be used by another program for reporting.
The static HTML would look something like this:
<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//thebigpartserver.com/stuff/q?partno=**PARTNO**&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe>
and I need to replace this:
{myplugin}ABCDEF1234,"Flux Capacitor"{/myplugin}
with that:
<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//thebigpartserver.com/stuff/q?partno=ABCDEF1234&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff">
</iframe>
Can this be done with a regex? Or is this making the regex to complicated?
If it is parsed and then using conditional logic outside the regex, wouldn't I need to save a pointer of where {myplugin}...{/myplugin} occurs since $article->text is a string?
Sure, that's what you use capturing groups for:
$re = '/\{myplugin\}(\w+),[^{}]+\{\/myplugin\}/';
$str = '{myplugin}ABCDEF1234,"Flux Capacitor"{/myplugin}';
$subst = '<iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//thebigpartserver.com/stuff/q?partno=\\1&link_opens_in_new_window=true&price_color=333333&title_color=0066c0&bg_color=ffffff"> </iframe>';
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Test it live on regex101.com.
Note: I'm assuming that part numbers consist of alphanumeric characters and are followed by a comma.