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>";
}"

Afiq Izzat
  • 69
  • 8
  • could you add your PHP? just explaining your code without showing us won't really help us help you fix your issue – Grey Dec 16 '16 at 08:10
  • why you dont replace it with empty space? $Travel=str_replace("&"," ",$Travel); – Rafael Shkembi Dec 16 '16 at 08:11
  • http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574 _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 – e4c5 Dec 16 '16 at 08:11

2 Answers2

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.
jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Actually, those data are categorized under "Insurance Type", so, you know. – Afiq Izzat Dec 16 '16 at 08:18
  • 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). – Afiq Izzat Dec 16 '16 at 08:20
  • @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. – jeroen Dec 16 '16 at 08:20
  • @AfiqIzzat Then you can easily decode them, see `htmlspecialchars_decode()`. – jeroen Dec 16 '16 at 08:21
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);
RichardAtHome
  • 4,293
  • 4
  • 20
  • 29