Skip to content
Advertisement

How to replace a single tag with multiple values in PHP?

I am sending a job openings email to my website users. I have the following sentences.

Dear user, 
The following jobs are recently posted in our website.
[JOBLIST]
Thank you.

If there is only one job, I can use str_replace("PHP Developer", "[JOBLIST]", $message)

But I have list of job openings say,

  • PHP Developer
  • Data Scientist
  • Data Entry

I would like to replace the above tag [JOBLIST] with the above job openings probably with a href link to the job details.

Advertisement

Answer

$jobs = array("PHP Developer", "Data Scientist", "Data Entry");
$links = array("phpdev","datasci","dataentry");

$str = "";
$n = count($jobs);
$temp = '<li><a href="https://example.com/jobs/{{link}}">{{job}}</a></li>';
$paras = array("{{link}}","{{job}}");

for($i=0;$i < $n; $i++){
   $str .= str_replace($paras,array($links[$i],$jobs[$i]),$temp);
}

$str = "<ul>".$str."</ul>";

$output = str_replace($str, "[JOBLIST]", $message);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement