PHP: How to insert a -tag after the @ inside an email address

I'm having problems with some email addresses that are too long. So I'd like to set a break after the @ character.

But unfortunately, the @ symbol seems to be a special character. That's why it doesn't work with:

str_replace ('@' , '@<br/>' , $email);

Is there any other way to get it done?

-------------Problems Reply------------

I'll make an attempt at hopefully answer this question, seeing that you are most likely not showing us your full code.

If by any chance that you're using another function in conjunction with this, say for example strlen(), then that could account for it, and how you're using it.

You may also be using another function, which is unknown at the time of your posting. Therefore you will need to elaborate on that, should my answer not provide you with a solution.

  • Far as I'm concerned, your "posted" code checked out, so I can't see how that could be failing.

Using the following and in conjunction with your posted code, proved to be successful.

<?php
// email with 33 characters
$email = "[email protected]";

// if more or equal to 33 characters
if(strlen($email) >= 33){
echo "The email <b>$email</b> contains 33 or more characters. ";
echo "Now putting them into 2 lines...";

echo "<br>";

// echo str_replace('@', '@<br/>', $email);
echo "First line: " . str_replace('@', '@<br/>Second line: ', $email);
}

else{
echo "Email is within character limit.";
}

Using echo strlen($email); will show you the email's string length.



Reference(s):

  • http://php.net/strlen
  • http://php.net/str_replace

Thank you for your help. Unfortunately "str_replace" does not work with the "@" character in my code. If I test it with the "." character it sometimes works. Ever time I reload the page in the browser the "str_replace" result changes.

Here's some more of the code. It's from a Wordpress plugin to show contact details of staff members. It loops through all staff posts.

$i = 0;
if( $staff->have_posts() ) {
if ($headline =='yes') {
$output .= '<h1 class="staff-group-mainheader">'.$maingroupname.'</h1>';
}
$output .= '<div class="staff-member-listing '.$group.'">';

while( $staff->have_posts() ) : $staff->the_post();
unset($staff_member_classes);

$terms = get_the_terms( $post->id, 'staff-member-group' ); // get an array of all the terms as objects.

$term_slug = array(); // save the slugs in an array
$term_name = array(); // save the slugs in an array
foreach( $terms as $term ) {
$term_slug[] = $term->slug; // this grabs the hyphenated slug
$term_name[] = $term->name; // this grabs the actual name
$staff_member_classes .= ' '.$term->slug;
}

if ($i == ($staff->found_posts)-1) {
$staff_member_classes .= " last";
}

if ($i % 2) {
$oddorevenpost = 'even';
} else {
$oddorevenpost = 'odd';
}

if (($oddorevenpost == $oddoreven) || ($oddoreven == '')) {

$output .= '<div class="staff-member '.$oddorevenpost.' '.$staff_member_classes.'">';

global $post;

$custom = get_post_custom();
$name = get_the_title();
$name_slug = basename(get_permalink());
$title = $custom["_staff_member_title"][0];
$function = $custom["_staff_member_fb"][0];
$email = antispambot($custom["_staff_member_email"][0]);

if (!empty($custom["_staff_member_phone"][0])) {
$phone = '<li><i class="icons lycon-phone"></i><span class="staff-phone">'.$custom["_staff_member_phone"][0].'</span></li>';
}
if (!empty($custom["_staff_member_mobile"][0])) {
$mobile = '<li><i class="icons icon-mobile-6"></i><span class="staff-mobile">'.$custom["_staff_member_mobile"][0].'</span></li>';
}
if (!empty($custom["_staff_member_fax"][0])) {
$fax = '<li><i class="icons lycon-fax"></i><span class="staff-fax">'.$custom["_staff_member_fax"][0].'</span></li>';
}
$company = $custom["_staff_member_company"][0];
$street = $custom["_staff_member_street"][0];
$city = $custom["_staff_member_city"][0];
$country = $custom["_staff_member_country"][0];
$bio = $custom["_staff_member_bio"][0];

if(has_post_thumbnail()){

$postidthumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID));
$photo_url = wp_get_attachment_medium_url( $postidthumb );
$photo = '<div class="staff-member-container" style="background: url('.$photo_url.') no-repeat right bottom;">';
}else{
$photo_url = '';
$photo = '';
}

if (function_exists('wpautop')){
$bio_format = '<div class="staff-member-bio">'.wpautop($bio).'</div>';
}
$emailbreak = str_replace ('@' , '@<br/>' , $email);

$email_mailto = '<li><i class="icons icon-mail-7"></i><span class="staff-email"><a class="staff-member-email" href="mailto:'.$email.'" title="Email '.$name.'">'.$emailbreak.'</a></span></li>';
$email_nolink = antispambot( $email );

$accepted_single_tags = $default_tags;
$replace_single_values = array($name, $name_slug, $photo_url, $title, $function, $email_nolink, $phone, $mobile, $fax, $company, $street, $city, $country, $bio, $fb_url, $tw_url);

$accepted_formatted_tags = $default_formatted_tags;
if ( $title =='') {
$formattedname = '<h3 class="staff-member-name">'.$name.'</h3>';
} else {
$formattedname = '<h3 class="staff-member-name">'.$title.' '.$name.'</h3>';
}
$replace_formatted_values = array($formattedname, '<h4 class="staff-member-position">'.$title.'</h4>', $photo, $email_mailto, $bio_format );

$loop_markup = str_replace($accepted_single_tags, $replace_single_values, $loop_markup);
$loop_markup = str_replace($accepted_formatted_tags, $replace_formatted_values, $loop_markup);

$output .= $loop_markup;

$loop_markup = $loop_markup_reset;

$output .= '</div> <!-- Close staff-member -->';
}
$i += 1;

endwhile;

$output .= "</div> <!-- Close staff-member-listing -->";
}
}

wp_reset_query();

$output = $style_output.$output;

Category:php Views:7 Time:2018-12-02
Tags: php

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.078 (s). 11 q(s)