hinklea hinklea - 1 month ago 8
PHP Question

Ignoring second instance of preg_replace

I'm doing a web scrape that generates random data.

The string from the random data format is

RestaurantJimmys Outdoor Grill


I have a
preg_replace
to remove Restaurant and keep the title.

However, when the name is
RestaurantSmith Family Restaurant
it turns into
Smith Family


What is the best solution to keep the second instance?

Code:

preg_replace('/Restaurant/', '', $restaurantName);


Thanks in advance!

Answer

I have tried two ways for this that works:

$str = "RestaurantSmith Family Restaurant";

echo preg_replace("/Restaurant(\w+)/", "$1", $str);

and:

echo preg_replace("/Restaurant\B/", "", $str);