phoenix phoenix - 16 days ago 6
PHP Question

Replace character after specific character in php

I have a string like this :

Hello-world-Hi-people-Hello-everybody


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

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:

<?php
$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.

Comments