/    Sign up×
Community /Pin to ProfileBookmark

how to use regular expression to find a value in a string

Hello people

I have a string which may contain the following HTML attribute:
colspan=”<value>”
for example: colspan=”5″
I need to be able to extract the value of the colspan value if one exists under the following conditions:
1) the colspan phrase is case insensitive
2) the double quote could be single quote or no quotes at all
3) the string may end after the colspan attribute or it may have other attributes (which I don’t care about). If there are other attributes after the colspan, at least one whitespace character can be assumed after the colspan value

The following are some valid examples:
colspan=”5″
colspan=’5′
COLspan=”5″
COLspan=5
colspan=’5′ width=”4″
colSPAN=5 nowrap

I would appreciate it if someone could help me with this as I am new to regular expression

regards

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@96turnerriJan 23.2005 — <i>
</i>&lt;?php
$str = //your html string
$colspan = preg_replace("/colspan=(.*?) /i", $1, $str);
$colspan = preg_replace("/'/", "", $str);
$colspan = preg_reaplce("/"/", "", $str);
echo $colspan;
Copy linkTweet thisAlerts:
@ShrineDesignsJan 23.2005 — if the syntax was XHTML or XML you could use the xml parser functions to get the value instead of regExp
×

Success!

Help @jasongr 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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