0

I am trying to split a string, and the string is (in MySQL):

"Accident (&amp); Travel;Car (&amp); Motorcycle."

You see, there are actually only two elements that I want to be in the array, [0] would be: "Accident & Travel, [1] would be the Car & Motorcycle".

I used a delimiter (explode) for this one and imposed a delimiter with (";").
Now, the problem is, the symbol "&" in MySQL is translated into (&amp); (without the bracket), meaning now my string is split like they have 4 elements instead of 2:

"[0] = Accident &, [1] = Travel, [2] = Car &, [3] = Motorcycle."

How do I solve this ? I heard of preg_split before, but I'm not sure how to use it.

The php explode that I used:
"$string = "Accident (&amp); Travel; Car (&amp); Motorcycle"; $points = explode(";", $string); ?> <ul> <?php for ($x = 0; $x < count($points); $x++) { echo "<li>$points[$x]</li>";
}"

3
  • could you add your PHP? just explaining your code without showing us won't really help us help you fix your issue Commented Dec 16, 2016 at 8:10
  • why you dont replace it with empty space? $Travel=str_replace("&"," ",$Travel); Commented Dec 16, 2016 at 8:11
  • stackoverflow.com/questions/3653462/… I store my data the totally wrong way, and now I'm having problems using it. It's complicated/difficult/too slow/doesn't work right! Can someone help? The answer is Yes - fix your data so it's stored properly to begin with, and all those problems using it go away. You don't have to optimize difficult things when they're not difficult in the first place Commented Dec 16, 2016 at 8:11

2 Answers 2

1

It's not really clear, but if I read the question correctly, the values are stored in the database without the brackets. So as:

Accident &amp; Travel;Car &amp; Motorcycle.

Then it would seem that the strings are encoded using something like htmlspecialchars before you store them.

To decode them, you could use htmlspecialchars_decode() before you explode.

However, this really shouldn't be necessary:

  • You should not encode your data for use in html before you store it in the database; you never know what you might need it for so you should probably store it as it is (after validation...). When you output it, you encode as the output medium requires.
  • You should not store multiple values in one database field; you should normalize your database, that will make data retrieval a lot easier.
Sign up to request clarification or add additional context in comments.

4 Comments

Actually, those data are categorized under "Insurance Type", so, you know.
And yes you are right about the values stored in the database (I just don't know how to escape the characters formatting on this website @@", I'm new).
@AfiqIzzat That doesn't really matter, they should still be in a separate table. A third table can then link entries from your original table to this new table.
@AfiqIzzat Then you can easily decode them, see htmlspecialchars_decode().
0
// original string
$string = "Accident &amp; Travel;Car &amp; Motorcycle.";

// replace the troublesome '&amp;' string
$string = str_replace('&amp;', '&', $string);

// split the string at the remaining ';'
$points = explode(";", $string);

1 Comment

Some explanation of what you are doing here would help understanding of your answer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.