Amy Neville
Amy Neville

Reputation: 10581

preg_match how do I extract this?

How do I write a regex to extract the "desired text" from the following:

data-zoom-image="desired text"

Upvotes: 0

Views: 63

Answers (4)

Prasanth Bendra
Prasanth Bendra

Reputation: 32730

$str  = 'data-zoom-image="desired text"';

preg_match('/data-zoom-image="(?P<text>\w+)"/', $str, $matches);

print_r($matches);

ref: http://php.net/manual/en/function.preg-match.php

Upvotes: 0

user1646111
user1646111

Reputation:

without quota:

substr($text, strpos($text, '"')+1, strrpos($text, '"') - strpos($text, '"')-1) ;

Upvotes: 0

DonCallisto
DonCallisto

Reputation: 29912

You don't need a preg_match for do this operation.

Simply you could use a substr in tandem with strpos

$find = substr($yourString,strpos($yourString,"="));

Upvotes: 1

rohitarora
rohitarora

Reputation: 1365

   preg_match('/(data-zoom-image=")(.*)(")/',$youstring,$match);

echo $match[2];

try this. it is a pattren

Upvotes: 1

Related Questions