Jump to content

[SOLVED] using preg_match to get a value


GuitarGod

Recommended Posts

Hello,

 

I'm working on some sort of BB code system, and I'm having a little bit of trouble with the colouring on font.

 

[color=??????][/color]

 

I need preg_match to check if a string has the code above, and, more importantly, return the value of ??*??*?? (without the *)

 

Any help is, as always, much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/82520-solved-using-preg_match-to-get-a-value/
Share on other sites

Ok, i figured part of it out:

 

if ( preg_match( '/\[color\=(.*?)\](.*?)\[\/color\]/is', $entry, $matches ) )

 

When i use 'print_r( $matches )', it works great. But if I used $match[0][0], it only returns the first character of what I want.

 

Any help?

ahh use preg_match_all

<?php
$entry = "[color=000000]some text[/color] and [color=FF0000]other text[/color]";

preg_match_all('%\[color\=(.*?)\](.*?)\[\/color\]%si', $entry, $result, PREG_SET_ORDER);
foreach($result as $r)
{
echo "color: ".$r[1]; //Color
echo "<br>text: ".$r[2]; //text
echo "<br>---<br>";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.