/    Sign up×
Community /Pin to ProfileBookmark

Using patterns for extracting n words from a string

Hi all,

I want to search within a string for patterns such as: <li> some text here… title=”word”
and retrieve a list of ‘word’s

Input: string with patterns such as the one shown above
Output: list of ‘word’ items

Comments welcome,
Ruben

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@scragarJan 24.2008 — unless you give examples this is about as close as I can guess:

$string = '&lt;li&gt;stuff "copy" &lt;/li&gt;
&lt;li&gt;meap "another coppied word"&lt;/li&gt;';
preg_match("/&lt;li&gt;.+"([^"]+)".+&lt;/li&gt;/", $string, $matches);
var_dump($matches);// note every 0dd index is what your after.

code tags prevent forum messing up regexps. - tested and fixed.
Copy linkTweet thisAlerts:
@rvazquezauthorJan 24.2008 — Here is an example:

Input

<li> stuff title="word1" </li>

<li> stuff title="word2" </li>

Output

array[0] = word1

array[1] = word2

Thanks,

Ruben
Copy linkTweet thisAlerts:
@scragarJan 24.2008 — [code=php]$x = '<li> stuff title="word1" </li>
<li> stuff title="word2" </li>';
preg_match_all("/[a-zA-Z]+="([^"]+)"/", $x, $matches);
print_r($matches[1]);[/code]
how's that?

you could even use it in a function to make it easier to reuse:
[code=php]function find_val($input){
preg_match_all("/[a-zA-Z]+="([^"]+)"/", $input, $matches);
return ($matches[1]);
};

$x = '<li> stuff title="word1" </li>
<li> stuff title="word2" </li>';
$something = find_val($x);
print_r($something);[/code]
Copy linkTweet thisAlerts:
@rvazquezauthorJan 24.2008 — Ok, good solution; but, I didn't tell this...

Input

stuff title="I dont want this text because it is not between tags"

<li> stuff title="word1" </li>

<li> stuff title="word2" </li>

Output

array[0] = word1

array[1] = word2


---
Ruben
Copy linkTweet thisAlerts:
@andre4s_yJan 24.2008 — Sorry for interruption... ?
[code=php]
<?php
$text="stuff title="I dont want this text because it is not between tags"n<li> stuff title="word0" </li>n<li> stuff title="word1" </li>";
preg_match_all("/<li>.+title="(.+)".*</li>/Uis",$text,$result);
print_r($result[1]);
?>
[/code]

it will output :

Array ( [0] => word0 [1] => word1 )
[/QUOTE]

Ready to cook.... yummy.... ?

But hey.. why you make 2 threads???

One is not enough?? or afraid no respond??
×

Success!

Help @rvazquez spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...