php - how to replace ... between a word where one part is numeric the other is'int?

238

how can i put a space between a word where one part is numeric the other is'int? like so:

$string = "this is a 2000px height photo";
$string = preg_replace('???',' //1',$string);
echo $string; //this is a 2000 px height photo

can someone help me? thanks!

40

Answer

Solution:

$string = preg_replace('/(\d+)([[:alpha:]]+)/', '$1 $2', $string);
  • \d+ matches any digit
  • [[:alpha:]]+ matches any letter (\w+ is wrong because matches letters and digits: thepreg_replace() call splits numbers - e.g. '100' becomes '10 0')

People are also looking for solutions to the problem: php - how to insert all this checkbox value into same id but different rows of one column?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.