Vince Lowe Vince Lowe - 2 months ago 8
PHP Question

strip left and right in php

I am converting the following python into php

the aim is to remove scores from a string like "Liverpool 1 v 0 Everton"

home, away = event_data.get("desc").split(' v ')

# remove scores from event desc
if home.rsplit(' ', 1)[1].isdigit() and away.split(' ', 1)[0].isdigit():
event_name = home.rsplit(' ', 1)[0] + " v " + away.split(' ', 1)[1]


in php so far

$nameArray = explode(' v ', $value['name']);

$home = $nameArray[0];
$away = $nameArray[1];

$event_name = $home . ' v ' . $away;


im struggling with the stipping the scores, any tips?

Answer

Using preg_repalce in PHP you can replace digits around " v ":

$str = "Liverpool 1 v 0 Everton";

$event_name = preg_replace('/\h+\d+\h+v\h+\d+\h+/', ' v ', $str);

echo $event_name . "\n";
//=> Liverpool v Everton
Comments