/    Sign up×
Community /Pin to ProfileBookmark

Using REGEX to alter some parts of a string

Hi Guys,
Is it me or is this VERY complicated.

Basically I have this string (this is an example there may be other strings with 50 emails in it):

[code=php]$string = ‘see listed on the guide please <a href=”mailto:[email protected]?subject=this is subject”>let us know</a>,
and if you would like to be listed then please <a href=”mailto:[email protected]?subject=Please list me”>contact us</a>.’;[/code]

and I want to convert it to:

[code=php]see listed on the guide please <span class=”mailme”>feedback(at)xxxxx(dot)com</span><span class=”mailme_extra”><span class=”subj”>this is subject</span><span class=”tit”>let us know</span></span>,
and if you would like to be listed then please <span class=”mailme”>ads(at)xxxxx(dot)com</span><span class=”mailme_extra”><span class=”subj”>Please list me</span><span class=”tit”>contact us</span></span>.<span class=”mailme”></span><span class=”mailme_extra”><span class=”subj”></span><span class=”tit”></span></span>[/code]

I did something to avoid regex, but its too long, wrong, and I am sure wont work:

I have been able to reach some results but not what I need below:

[code=php] function safe_emailto($content) {
$email_patt = ‘([A-Za-z0-9._&#37;-]+)@([A-Za-z0-9._%-]+).([A-Za-z0-9._%-]+)’;
$mailto_pattern = ‘#<a[^>]*?href=”mailto:%5cs?’ . $email_patt . ‘?subject=([A-Za-z0-9._%-]+)[^>]*?>[^>]*?</a>#’;
$rewrite_result = ‘<span class=”mailme”>$1 AT $2 DOT $3</span><span class=”mailme_extra”><span class=”subj”>$4</span><span class=”tit”>$5</span></span>’;

$content = preg_replace($mailto_pattern, $rewrite_result, $content);
$content = preg_replace(‘#’ . $email_patt . ‘#’, $rewrite_result, $content);

return $content;
}[/code]

Can someone help me out here.

Basically wont show $5 and $4 only works if there is no space between the subject.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@CharlesNov 25.2010 — Use DOMDocument instead of reguar expressions. REs are great for a lot of things but nor for manipulating HTML.
Copy linkTweet thisAlerts:
@XeroSiSauthorNov 28.2010 — Yeah but in this curtain case we need to go with REGEX, is there no way to play around with our code?
Copy linkTweet thisAlerts:
@CharlesNov 28.2010 — Is there no way to play around with our code?[/QUOTE]Not really, no. Just one example, you are not even close to describing an e-mail address. Best to overcome whatever it is that is keeping you from using DOMDocuent.
Copy linkTweet thisAlerts:
@NogDogNov 28.2010 — This appears to be pretty close to what you want:
[code=php]
$string = 'see listed on the guide please <a href="mailto:[email protected]?subject=this is subject">let us know</a>,
and if you would like to be listed then please <a href="mailto:[email protected]?subject=Please list me">contact us</a>.';
$regexp = '#<as[^>]*href=(['"])mailto:(.+?)?(.+?)?1[^>]*>(.*?)</a>#is';
$string = preg_replace_callback($regexp, 'callback', $string);
echo $string;

function callback($matches)
{
$email = str_replace(array('@', '.'), array('(at)', '(dot)'), $matches[2]);
$html = "<span class='mailme'>$email</span><span class='mailme_extra'>";
if(!empty($matches[3])) {
$vars = explode("&", $matches[3]);
foreach($vars as $var) {
list($key, $value) = explode('=', $var);
$html .= " <span class='$key'>$value</span>";
}
$html .= " <span class='title'>$matches[4]</span></span>";
}
return $html;
}
[/code]
Copy linkTweet thisAlerts:
@XeroSiSauthorNov 29.2010 — amazing! Thanks a lot.
×

Success!

Help @XeroSiS 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.13,
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,
)...