freaky
freaky

Reputation: 1010

php regex attribute from string

I've got a string like this:

family="ABeeZee;100regular" decoration="" style="normal" txtalign="txt-left" size="14px" line="22px" padding="0px" bold="" uppercase="" color=""" 

And I want just to get the value/string inside family:

 ABeeZee;100regular

I know that I need to use regex but I still don't understand how to write the pattern...

I tried this without success:

$mystring = 'family="ABeeZee;100regular" decoration="" style="normal" txtalign="txt-left" size="14px" line="22px" padding="0px" bold="" uppercase="" color=""" '
$pattern  = '/(family)="([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)"/is';
$string   = preg_replace($pattern, "", $mystring);

Upvotes: 0

Views: 30

Answers (1)

hwnd
hwnd

Reputation: 70732

Use preg_match() instead of preg_replace() to grab the value.

You only have a single pattern so you don't need to use a conditional regex here.

preg_match('/family="([^"]+)"/', $mystring, $match);
echo $match[1]; //=> "ABeeZee;100regular"

Upvotes: 2

Related Questions