phoenix phoenix - 1 year ago 101
PHP Question

Replace character after specific character in php

I have a string like this :


And i want to str-replace '-' by spaces and have this result :

Hello-world Hi-people Hello-everybody

I have tried :

str_replace('-', ' ', $mystring);

But this is not what i'm looking for

Mik Mik
Answer Source

Maybe instead of trying to select "-", focus on which strings you want to isolate? For instance, is it "Hello" or "Hi" followed by one word? Then this could work:

$input = "Hello-world-Hi-people-Hello-everybody";
preg_match_all("/(Hello|Hi)-\w*/", $input, $output);
echo implode(" ", $output[0]);

I'm not very efficient with regex myself, but this could show the way.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download