/    Sign up×
Community /Pin to ProfileBookmark

extract string between 2 tags/words

I want to extract each word between 2 given tags/words in my string (from text file).

For example my string is:

“Lorem ipsum <b>[B]dolor[/B]</b> sit amet, consectetur adipiscing elit. <b>[B]Nulla[/B]</b> molestie, orci at eleifend ullamcorper, nulla <b>[B]diam[/B]</b> dictum purus, et mattis leo risus id metus. <b>[B]Proin[/B]</b> id pretium enim.”

I want extract (the best to array) all words between “[B]<b>[/B]” and “[B]</b>[/B]“, so array would be:

[B]dolor, Nulla, diam, Proin[/B]

Is this possible?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Peterk92Nov 07.2010 — Im not exactly sure how to do it but i believe you should be looking at the DOM functions in PHP
Copy linkTweet thisAlerts:
@NogDogNov 07.2010 — You can use [url=http://php.net/preg_match_all]preg_match_all[/url](), though it can potentially be problematic if you have nested tags for some reason, e.g.
<i>
</i>This &lt;b&gt; is &lt;b&gt;a&lt;/b&gt; test&lt;/b&gt;.

A more robust solution is to use something like the [url=http://php.net/dom]DOM extension[/url], assuming the source text is reasonably valid HTML, then use the DOMDocument::getElementsByTagName() method.
Copy linkTweet thisAlerts:
@HelleshternauthorNov 07.2010 — I see, I'll check it out (however there won't be nested tags).
×

Success!

Help @Helleshtern 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...