samedi 9 mai 2015

Use regular expression to extract attribute value for custom tag

Thanks for taking a look at this. I'm using PHP. I have a string like so:

[QUOTE="name: Max-Fischer, post: 486662533, member: 123"]I don't so much dance as rhythmically convulse.[/QUOTE]

And I want to pull out the values in the quotes and create an associative array like so:

["name" => "Max-Fischer", "post" => "486662533", "member" => "123"]

Then, I would like to remove the opening and closing [QUOTE] tags and replace them with custom HTML like so:

<blockquote><a href="URL_I_WILL_GENERATE_FROM_THE_ARRAY_VALUES">Max-Fischer</a> wrote: I don't so much dance as rhythmically convulse.</blockquote>

So the main problem is creating the preg_match() or preg_replace() to handle first: grabbing the values out in an array, and second: removing the tags and replacing them with my custom content. I can figure out how to use the array to create the custom HTML, I just can't figure how to use regular expressions well enough to achieve it.

I tried a match like this to get the attribute values:

/(\S+)=[\"\']?((?:.(?![\"\']?\s+(?:\S+)=|[>\"\']))+.)[\"\']?/

But this only returns:

[QUOTE

And that's not even addressing how to put the values (if I can get them) into an array.

Thanks in advance for your time.

Cheers.

Aucun commentaire:

Enregistrer un commentaire