I'm working in Joomla and need to change text on the fly based on the current URL so that I can create "area pages" for my services. I'm using a module that allows me to insert PHP so this is what i'm working with;
This is the code I copy and pasted together;
<?php $activeURL=basename($_SERVER['REQUEST_URI']);
if (strpos($activeURL, 'paddington') !== false) { $area='Paddington';}
if (strpos($activeURL, 'clapham') !== false) { $area='Clapham';}
if (strpos($activeURL,'brixton') !== false) { $area='Brixton';}
echo $area . '<br/>' . "\n";
echo 'SysFix provide a wide range of computer repair services throughout ';
echo $area;
echo ', London and the UK. A member of our highly skilled, technical support team will visit you at home to provide assistance with you computer issues.';
echo '<br/>' . "\n";
echo '<br/>' . "\n";
echo 'As well as on site computer repairs, we also provide technical support over the internet using our remote support software which not only saves you money but is quicker too.';
What i'm doing is looking at the URL and if it contains a word (paddington, clapham or brixton), then set a variable to that effect with a capital letter and then echo that variable throughout my static text (article) so that it seems less like duplicate content as refers to an area.
CAVEAT I have around 100 areas so don't want all this code. I'm looking for a way to maybe use regex instead but sorry, i've no idea how to use it. Hoping there's an expert.
All my URLs are formed like this;
https://www.sysfix.co.uk/Computer-Repair/clapham-sw4.html
https://www.sysfix.co.uk/Computer-Repair/brixton-sw2.html
https://www.sysfix.co.uk/Computer-Repair/marylebone-w1-w1m-nw1.html
so is there a better way to do this instead of creating 100 of these;
if (strpos($activeURL, 'paddington') !== false) { $area='Paddington';}
for every area page. I guess i'm looking to extract the location name after Computer-Repair/AREANAME AND DISCARD THE REST after the next - (also making the area name a capital letter.
I can then subsequently echo $area. This i need an if set statement too in case there's a page that has a url that does not conform?
You may use
'~/Computer-Repair/\K[\w-]*?(?=-[a-z]*\d|$)~'
See the regex demo.
Details
/Computer-Repair/
- a literal subtring\K
- a match reset operator that omits the currently matched text[\w-]*?
- 0+ word or -
chars as few as possible, up to the first...(?=-[a-z]*\d|$)
- -
, then 0+ lowercase ASCII letters ([a-z]*
) and then any digit (\d
) or (|
) the end of string ($
).See the PHP demo:
$strs = ['/Computer-Repair/lancaster-gate-w2.html', '/Computer-Repair/marylebone-w1-w1m-nw1.html', '/Computer-Repair/covent-garden-wc2.html', 'https://www.sysfix.co.uk/Computer-Repair/clapham-sw4.html','https://www.sysfix.co.uk/Computer-Repair/brixton-sw2.html', 'https://www.sysfix.co.uk/Computer-Repair/marylebone-w1-w1m-nw1.html'];
foreach ($strs as $s){
if (preg_match('~/Computer-Repair/\K[\w-]*?(?=-[a-z]*\d|$)~', $s, $m)) {
echo preg_replace_callback('~(-)?\b(\w)~', function ($x) {
return (isset($x[1]) && strlen($x[1]) ? " " : "") . ucfirst($x[2]);
}, $m[0]) . "\n";
}
}
Output:
Lancaster Gate
Marylebone
Covent Garden
Clapham
Brixton
Marylebone
Note that ucfirst($x[2])
will make the first letter uppercase.