2

I have an string email address that comes from user input($option['footer_email']) and I want to replace it with mailto html tag. So far I tried couple of approaches and tried to use str_replace() as followed, but it didn't work out.

$email = array($option['footer_email']);
$replace = array('<a href="mailto:$option['footer_email']">$option['footer_email']</a>');
echo str_replace($email, $replace, $option['footer_email']);

and it returns syntax error, unexpected 'footer_email' (T_STRING), expecting ')'

I also tried to use preg_replace() but that one didn't help. How can I fix this issue?

Korbin
  • 578
  • 5
  • 18

4 Answers4

2

Try this

$email = $option['footer_email'];
$replace = '<a href="mailto:'.$option['footer_email'].'">'.$option['footer_email'].'</a>';
echo str_replace($email, $replace, $option['footer_email']);

Basically your variable is not read as it is part of the string.

Why do you have to do the str_replace? Why not just do this?

echo '<a href="mailto:'.$option['footer_email'].'">'.$option['footer_email'].'</a>';
Uberswe
  • 1,038
  • 2
  • 16
  • 36
2

please check your Apostrophe opening and closing properly, here this is working example with a dummy value

$option['footer_email'] = 'test@test.com';
$email = array($option['footer_email']);
$replace = array("<a href='mailto:$option[footer_email]'>$option[footer_email]</a>");
echo str_replace($email, $replace, $option['footer_email']);
Sylvester
  • 388
  • 4
  • 12
2
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$email = array($option['footer_email']);
$replace = array('<a href="mailto:"' . $option['footer_email'] . '">' .$option['footer_email'] . '</a>');
echo str_replace($email, $replace, $option['footer_email']);
Prashant G Patil
  • 927
  • 1
  • 8
  • 22
1

Try This

$option['footer_email']="waseem@waseem.com";
    $email = array($option['footer_email']);
    $replace = '<a href="mailto:'.$option['footer_email'].'">'.$option['footer_email'].'</a>';
    $replace_val= str_replace($email, $replace, $option['footer_email']);
    //check By var_dump
    var_dump($replace_val);
    //check By Print_r
    print_r($replace_val);
    //check By echo
    echo $replace_val;
Waseem Ahmad
  • 303
  • 3
  • 10