hinklea hinklea - 1 year ago 71
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
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?


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

Thanks in advance!

Answer Source

I have tried two ways for this that works:

$str = "RestaurantSmith Family Restaurant";

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


echo preg_replace("/Restaurant\B/", "", $str);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download