Search code examples
phpregexpreg-replaceexplodepreg-split

I need to find a way explode a specific string that has quotes in it


I'm having serious trouble with this and I'm not really experienced enough to understand how I should go about it.

To start off I have a very long string known as $VC. Each time it's slightly different but will always have some things that are the same. $VC is an htmlspecialchars() string that looks something like

<a href="example.com?continue=pid%3D6057413202557366578%26oid283447094297409">Example Link</a>... Lots of other stuff in between here... 80] ,[] ,"","3245697351286309258",[] ,["812750926... and it goes on ...80] ,[] ,"","6057413202557366578",[] ,["103279554... and it continues on

In this case the <a> tag is always the same so I take my information from there. The numbers listed after it such as ,"3245697351286309258",[] and ,"6057413202557366578",[] will also always be in the same format, just different numbers and one of those numbers will always be a specific ID. I then find that specific ID I want, I will always want that number inside pid%3D and %26oid.

$pid = explode("pid%3D", $VC, 2);
$pid = explode("%26oid", $pid[1], 2);
$pid = $pid[0];

In this case that number is 6057413202557366578. Next I want to explode $VC in a way that lets me put everything after ,"6057413202557366578",[] into a variable as its own string.

This is where things start to break down. What I want to do is the following

$vinfo = explode(',"'.$pid.'",[]',$VC,2);
$vinfo = $vinfo[1]; //Everything after the value I used to explode it.

Now naturally I did look around and try other things such as preg_split and preg_replace but I've got to admit, it is beyond me and as far as I can tell, those don't let you put your own variable in the middle of them (e.g. ',"'.$pid.'",[]').

If I'm understanding the whole regular expression idea, there might be other problems in that if I look for it without the $pid variable (e.g. just the surrounding characters), it will pick up the similar parts of the string before it gets to the one I want, (e.g. the ,"3245697351286309258",[]).

I hope I've explained this well enough, the main question though is - How can I get the information after that specific part of the string (',"'.$pid.'",[]') into a variable?


Solution

  • I hope this does what you want:

    pid%3D(?P<id>\d+).*?"(?P=id)",\[\](?P<vinfo>.*?)}\);<\/script>
    

    It captures the number after pid%3D in group id, and everything after "id",[] (until the next occurence of });</script>) in group vinfo.

    Here's a demo with shortened text.