I need to list a text field with values separated by ;

Asked

Viewed 104 times

2

I have a field text that is being inserted in the same phones in that format:

11 2222-3333;12 9 1111-2222;18 1111-2222;11 2222-3333

And I need to list these fields as a simple select in line and spine. the result I need is this:

11 2222-3333
12 9 1111-2222
18 1111-2222
11 2222-3333

3 answers

4

Bring the data to your SQL and put in a variable the part of the phones, in my example was in the $str and follow the code below:

<?php
   $str = '11 2222-3333;12 9 1111-2222;18 1111-2222;11 2222-3333';
   $strs = explode(';', $str);
   foreach($strs as $st){
      echo $st;
      echo PHP_EOL;
   }

Example: ideone

Note: do the conversions in PHP, which is the best solution

1

There are several ways to do, one option would be to make a explode() and then implode(), example:

$str = '11 2222-3333;12 9 1111-2222;18 1111-2222;11 2222-3333';
echo implode('<br/>',explode(';',$str));

Or str_replace()

$str = '11 2222-3333;12 9 1111-2222;18 1111-2222;11 2222-3333';
echo str_replace(';','<br/>',$str);
  • ok in php works but need it to be specifically in a mysql select

  • @user7520, mysql does not have native function to break string into type lines explode PHP, what you can do is create a procedure to count the number of separators and take the values with SUBSTRING_INDEX, see a simple example http://sqlfiddle.com/#! 2/a1e1e/11

  • @user7520 maybe this will also help you http://gustavostraube.wordpress.com/2012/12/split-string-no-mysql/

0

I would use str_replace:

$str = '11 2222-3333;12 9 1111-2222;18 1111-2222;11 2222-3333';
echo str_replace(';', '<br />', $str);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.