/    Sign up×
Community /Pin to ProfileBookmark

Search for Custom Tag and Get its Value

Hi guys,

I hope you guys can help me.

I am looking for ways to extract the value, of a custom tag, say for example [%TagValue%] in a given string.

For example:
$string = “I am a cool string and I am looking for [%TagValue1%] and [%TagValue2%].”;

I am looking search the number of occurances of a custom tag [%~%] as well as extract its value.

Really, hope you guys can help me.

Thanks in advance.

Farhan

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 11.2011 — preg_match() and/or preg_match_all() will be your friend.
Copy linkTweet thisAlerts:
@tirnaFeb 11.2011 — You also will need to understand regexp's.

This extracts whatever is in between the [% and %] tags in a string.

$matchesArray is a 2D array with $matchesArray[0] containing the strings between the tags.

[code=php]
<?php
$string = "[%TagValue4%]I am a cool string and I am looking for [%TagValue1%] and xxx [%TagValue2%][%TagValue3%] more text.";
preg_match_all("/(?<=([%))[^(%])]+/", $string, $matchesArray, PREG_PATTERN_ORDER);
foreach ($matchesArray[0] as $value) {
echo $value . '<br/>';
}
?>
[/code]
×

Success!

Help @farhan_ghazali 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.19,
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,
)...