PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to add HTML tag to the last word of a string?

 November 20, 2022     php, preg-replace, regex, replace, string     No comments   

Issue

How can add I HTML tags ( <b> </b> ) to the last word of a string?

The word is different for each result.

Language used : PHP The string is a 1 line string ( the local market of city ) and inserted in a link

echo '<a href="'.$marche_permalink.'">'. $marche_title . '</a> ['. $hdebut .' - '. $hfin .']<br />';

The string is $marche_title.


Solution

I think the simplest interpretation of your question is this:

$formatted = preg_replace('/\S+$/', '<b>$0</b>', $string);

It will wrap any sequence of non-whitespace characters before the end of the string in the <b> tag. If there aren't any spaces, it will wrap the entire string in the tag, which should be okay because the entire string is the last word of the string.



Answered By - Don't Panic
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing