0

I have more than 2 lac records in DB like this: [u'jazz', u' female vocalists', u' vocal jazz', u' jazz vocal'] I need to parse out each record via PHP.

for example,

$tags="[u'jazz', u' female vocalists', u' vocal jazz', u' jazz vocal']";

I need to parse out 4 individual little tags, as:

  • jazz
  • femal vocalists
  • vocal jazz
  • jazz vocal
5
  • What is a "lac record"? How did you get $tags? Is that from a single database column? Commented Mar 14, 2014 at 4:30
  • is the u'jazz', a typo/cut and paste failure or does every entry start with u ? Commented Mar 14, 2014 at 4:30
  • Looks like a Python list of unicode strings. A simple regex would do instead of parsing. Commented Mar 14, 2014 at 4:32
  • yes, its single database colunm @Mike Commented Mar 14, 2014 at 10:18
  • @GOOG Then you have a design problem. You should store your tags in a separate table instead of some sort of string of text like that. This will save you many headaches down the road. Commented Mar 15, 2014 at 0:49

1 Answer 1

2

Use preg_match_all

<?php
$tag="[u'jazz', u' female vocalists', u' vocal jazz', u' jazz vocal']";
preg_match_all("~'(?:\s?([^']+))'~",$tag,$matches);
print_r($matches[1]);

OUTPUT

Array
(
    [0] => jazz
    [1] => female vocalists
    [2] => vocal jazz
    [3] => jazz vocal
)
Sign up to request clarification or add additional context in comments.

1 Comment

suppose I have like this [u'sweden', u' black', u" rock'n'roll", u' hellacopters'] I need to parse out 4 individual little tags, as: 1. sweden 2. black 3. rock n roll 4. hellacopters

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.