Forum Moderators: coopster

Message Too Old, No Replies

str replace or preg?

Cannot figure it out

         

henry007

7:27 am on Apr 6, 2025 (gmt 0)

Top Contributors Of The Month



If i do this:
$ingredients = str_replace("&#10", "<br>", $ingredients);
$ingredients = str_replace("&#39", "<br>", $ingredients);
$ingredients = str_replace(";", "", $ingredients);
It works, how to make it only one line?

My problem is that when I copy and paste a text from any text editor in a form MySQl renders it's value with &#39 etc...
any solution?
thanks

henry007

9:13 am on Apr 6, 2025 (gmt 0)

Top Contributors Of The Month



I guess I figured ot out, insted of dealing with the php side I have added <pre></pre> in he html output
it seems to work OK
your thaughts?

lucy24

4:14 pm on Apr 6, 2025 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Granted, I only know about three words of php, but isn't there some kind of "decode" command that converts all encoded characters into their “real” form in one fell swoop? If javascript can do it, php ought to.

henry007

5:01 pm on Apr 6, 2025 (gmt 0)

Top Contributors Of The Month



Yes indeed, but I am not able to figure it out

PS) However the <pre> trick works fine

henry007

1:12 pm on Apr 8, 2025 (gmt 0)

Top Contributors Of The Month



great thanks a lot :)

explorador

9:34 pm on May 26, 2025 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm late to the party.

Why do you want that in one line? there is a way, but it's not worth it.

preg_replace can solve it at once, but it's not as efficient as str_replace. I read about this trying to optimize a system, and it's true, you may find diverse results, but at least I tried diff options with diff php versions and I could see better results with str_replace (regardless of using 1 or 3 lines). Just beware that str_replace doesn't work as Perl replacing text, as it's not fully recursive (I tried this myself with diff scenarios).

So, at the end of the day, there is really nothing wrong with your original approach.

lucy24

3:58 pm on May 27, 2025 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Looking back over the thread, it puts me in mind of times when “two steps forward, one step back” turns out best. Instead of working up some horribly convoluted code to ensure that only certain things are changed, it’s easier to first change everything and then--using a much simpler pattern--change some of them back.

Jonesy

7:07 pm on Jun 4, 2025 (gmt 0)

10+ Year Member Top Contributors Of The Month



@lucy24 -- especially when you revisit the 'compressed' code a year later and have no memory of WTF it was supposed to do.(Even with comments -- if there even are comments.)