Coder
Coder

Reputation: 339

How to replace only string end with number - preg_replace php

Here is my string.

$text = 'I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';

I want to replace [pause = "3000"] by <break time="3000ms">

I have written following regex but it is selecting till the last "]

\[pause.*\"(\d+)\".*\"]

PHP : $text = preg_replace("/\[pause.*\"(\w+)\".*\"]/", '<break time="$1ms"/>', $text);

If I were to find a solution where the regular expression only selects 'any number'

any number"]

my problem would be solved.

But I am not able to find how to do it.

Do you have any suggestions ?

Upvotes: 2

Views: 1370

Answers (2)

The fourth bird
The fourth bird

Reputation: 163257

Your regex matches too much due to the last part .*\" If you remove that part you would have your match for the current example data, but the first .* still matches any character including for example characters like "[].

What you could do is replace that first .* by matching the equals sign surrounded by horizonal whitespace character like \h*=\h*

Note that you don't have to escape the double quote.

You might use:

\[pause\h*=\h*"(\d+)"]

Regex demo

That will match

  • \[pause Match [pause
  • \h* Match zero or more horizontal whitespace characters
  • = Match =
  • \h*" Match zero or more horizontal whitespace characters followed by a "
  • (\d+) Capture in a group one or more digits
  • "] Match "]

And replace with:

<break time="$1ms"> or use <break time="$1ms"/>

For example:

$text = 'I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';
$text = preg_replace('/\[pause\h*=\h*"(\d+)"]/', '<break time="$1ms"/>', $text);
echo $text;

Demo

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You may use

\[pause[^]]*"(\d+)"]

Or (if there may be something else after the digits):

\[pause[^]]*"(\d+)"[^]]*]
                   ^^^^^

and replace with <break time="$1ms"/>. See the regex demo

Details

  • \[pause - [pause substring
  • [^]]* - 0+ chars other than ]
  • " - a double quotation mark
  • (\d+) - Group 1: one or more digits
  • "] - a "] substring.

PHP demo:

$str = 'I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry [pause ="3000"] i can not hear you ?[rate="-70.00%"] i still can\'t hear you[endrate] ? [rate="+50.00%"]why i can\'t hear you[endrate] ?';
echo preg_replace('~\[pause[^]]*"(\d+)"]~', '<break time="$1ms"/>', $str);
// => I am  [pitch = "high"]Rudratosh Shastri[endpitch]. what is your name ? how  are you? sorry <break time="3000ms"/> i can not hear you ?[rate="-70.00%"] i still can't hear you[endrate] ? [rate="+50.00%"]why i can't hear you[endrate] ?

Upvotes: 2

Related Questions