-1

I have a string inside another to which I need to append something.

The structure of string is something like this:

$output = "Family: peppers, dimensions: 150 cm, origin: South America, Pot diameter: 14, height with pot: 80";

In that string I need to find Pot diameter and append to its numeric value the string cm. so it will look like

Pot diameter: 14 cm.

In the site it appears like this:

Family: peppers.
Dimensions: 150cm
Origin: South America
Pot diameter: 14
Height with pot: 80

I have been trying to use preg_replace() via

$output = preg_replace("/Pot diameter: [0-9]+/", '\0 cm.', $output)

but it doesn't work at all. I can't figure out how to find a number within given string - an exact number won't work because it's used dynamically. Only letters inside this string are static.

I figure out that Source code shows:

<div><br/>
<b>Średnica doniczki: </b>
14<br/>
</div>

Changing to:

$output = preg_replace("/<b>Średnica doniczki: </b> <br/>[0-9]+/", '\0 cm.', $output);

Dosen't work either.

I have now solution

$output = preg_replace("/<b>Średnica doniczki: <\/b>(.*?)<\/div>/", 'Średnica doniczki: $1 cm.</div>', $output);

I just picked the next closing tag, and find anything between given string and ending tag, and then just inject the same string with first taken value and it works as i wanted to do.

12
  • The matching is case-sensitive. You've got pot in the string and Pot in the function, so it doesn't match. You also need $ instead of % for a variable name in PHP. Fix that and it works fine - demo: 3v4l.org/mpCvp . Voting to close this question as a typo, because those issues are trivial and have nothing to do with the question supposedly being asked.
    – ADyson
    Apr 26, 2022 at 9:34
  • P.S. See also How do I format my posts? to help you with the presentation of your future questions and answers. Thanks.
    – ADyson
    Apr 26, 2022 at 9:39
  • Those are just misspellings, variables and letter case have no matter here. Preg_replace just seems like it dosen't see that string. Here is site where i'm trying to prepend e-botanica.pl/p/rhaphidophora-decursiva-dragon-tail Here is exacly what im trying to do: $output = preg_replace("/Średnica doniczki: [0-9]+/", '\0 cm.', $output); And it dosen't add it at all
    – Kacper
    Apr 26, 2022 at 9:42
  • I just showed you a demo of your code working when you fix the letter case.
    – ADyson
    Apr 26, 2022 at 9:43
  • 1
    I know, i just figure out that source code shows diffrent, so it should containts tags inside that string actually. I will try fix it by myself then
    – Kacper
    Apr 26, 2022 at 9:59

1 Answer 1

1

I solved my problem via

$output = preg_replace("/<b>Średnica doniczki: <\/b>(.*?)<\/div>/", 'Średnica doniczki: $1 cm.</div>', $output);

To do so I just picked the next closing tag,
In the next step I find anything between given string and ending tag,
Next I just inject the same string what is given with first taken value and closing tag
And now it works as i wanted to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.