1

i got a big string as shown below(number of sets of data is not known):

  p '5123': {
            p 'tmp': p '1', p 'name': p 'mango', p 'abc': p 'abcd4 http://mysite/items/1234', p 'number': p '1123', p 'itemCategory_title': p 'fruits', p 'logo': p '2123.png', p 'itemCategory_id': p '90'
        }, p '700': {
            p 'tmp': p '0', p 'name': p 'cherry', p 'abc': p 'abcd4 http://mysite/items/1235', p 'number': p '1124', p 'itemCategory_title': p 'fruits', p 'logo': p '2124.png', p 'itemCategory_id': p '91'
        }, p '800': {
....................

i want to extract the following data from it and create a hyperlink for each set of data! (extract following values: tmp,name,abc,number,itemCategory_title,logo,itemCategory_id)

could any one tell me how i can extract the following data ?Thanks in advance.

sample one set of data to extract :

tmp=1
 name=mango
 abc=abcd4 http://mysite/items/1234
 number=1123
 itemCategory_title=fruits
 log=2123.png
 itemCategory_id=90

output this html hyperlinks via php:

<a href="./process.php?tmp=1&name=mango&abc=abcd4 http://mysite/items/1234&number=1123&itemCategory_title=fruits&log=2123.png&itemCategory_id=90">mango </a> <br />
<a href="./process.php?tmp=1&name=cherry&abc=abcd4 http://mysite/items/1235&number=1124&itemCategory_title=fruits&log=2124.png&itemCategory_id=91">cherry </a> <br />

code2 holds the sample input string:

    $code2 = stripslashes($_POST['outputtext']);
$clean_str = str_replace("p '","'",$code2);

$serialized = serialize($clean_str);
$expected_array = unserialize($serialized);
print_r($expected_array);


      // iterate the new array
    for($i = 0; $i < count($expected_array); $i++){
    ?>
    <a href="./process.php?tmp=<?php  echo $expected_array['tmp'] ; ?>&name=<?php  echo $expected_array['name'] ; ?>&abc=<?php  echo $expected_array['abc'] ; ?>&itemCategory_title=<?php  echo $expected_array['itemCategory_title'] ; ?>&log=<?php  echo $expected_array['log'] ; ?>&itemCategory_id=<?php  echo $expected_array['itemCategory_id'] ; ?>"><?php  echo $expected_array['itemCategory_title'] ; ?> </a> <br />
    <?
    }
1
  • Thanks for reply . i don't know how to use preg_match_all for this type of data! Commented Nov 19, 2015 at 11:55

1 Answer 1

0

You can use a trick here. If you know the p is common everywhere then remove it from the string.

$str = "{ p '5123': {
            p 'tmp': p '1', p 'name': p 'mango', p 'abc': p 'abcd4 http://mysite/items/1234', p 'number': p '1123', p 'itemCategory_title': p 'fruits', p 'logo': p '2123.png', p 'itemCategory_id': p '90'
        }, p '700': {
            p 'tmp': p '0', p 'name': p 'cherry', p 'abc': p 'abcd4 http://mysite/items/1235', p 'number': p '1124', p 'itemCategory_title': p 'fruits', p 'logo': p '2124.png', p 'itemCategory_id': p '91'
        } }";
$clean_str = str_replace("p '","'",$str);

Now it seems like json string;

$expected_array = json_decode($clean_str);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks pratik soni . i tried to use your method but clean_str is showing empty using this method $clean_str = preg_replace("/p\s'","'",$code2); echo $clean_str; AND textarea is empty <textarea rows="30" cols="150"><?PHP echo $clean_str; ?></textarea> i want to see the clean_str data before i deal with the array
I have edited the code. Replaced preg_replace with str_replace. It should work now.
Thanks now it removed the p values. How to reference each array item so i use it inside for loop to construct the hyperlinks ?

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.