How to highlight the whole word in bold [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
There is a suggestion: teg are divided into two categories.
code:
echo preg_replace("/($word)/i","<b>$1</b>", htmlspecialchars($words[i])."<br>";
If a word, such as a teg, only highlights it in bold. How to highlight an entire word, using the code above, highlights only part of the category

From the given context, you can perform a match of the word you are looking for with matching optional non-space characters before or after it surrounded by a word boundary like below:
<?php
$str = 'HTML-document';
$word = 'HTML';
echo preg_replace('/\b[^\s]*'.preg_quote($word).'[^\s]*\b/',"<b>$0</b>", $str);
Online Demo

Related

How to insert a dash at a specific position of a string in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a string for example 'AA231CS'and I want to add a hyphen or dash so it looks like this (the results should be this) 'AA-231-CS'.
and another string '9999ZZZ99' and I want to add a hyphen or dash so it looks like this (the results should be this) '9999-ZZZ-99'.
what's the best approach to handle this?
thank you!
Here is regex required and implode
$string = "9999ZZZ99AAA";
// I have separated string with continous numbers and alphabets in group
preg_match_all('/([0-9]+|[a-zA-Z]+)/',$string,$matches);
// imploding them by `-`
echo implode("-", $matches[0]);
Demo.

REGEX for alternate characters in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a string mystring. I have to replace the alternate characters with x so that final output is mxsxrxnx. All the alternate characters starting from 2nd position are replaced with x. I can do it with loop but is there a Regular expression for that or a better way in PHP? Please help.
Using a reset match meta-character \K you are able to do it:
.\K.
Live demo
PHP:
echo preg_replace('/.\K./', 'x', 'mystring'); // mxsxrxnx

Check if string contains a character combination and replace if it does in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have possible string combinations:
$string1 = '[variation-price] for product one.';
$string2 = 'Product two consists of [variation-parts]';
$string3 = 'Simple product';
$replace = 'Contains variations';
I need to check every string if it contains [variation- and if it does, replace the whole word (f.x. [variation-price]) with $replace.
I have tried various functions, bet the problem is that I do not know where exactly in a string variation may appear or how long it will be.
Any help or guidance is much appreciated.
Use \[variation-.*?\] regex with preg_replace():
preg_replace("/\[variation-.*?\]/", $replace, $string1)
Demo: https://ideone.com/MtNZ3L

preg_match pattern with signal character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need a pattern which is able to find a word wrapped in two signal characters.
Basically something like
$string = "bablabla __test__ blablabla"
preg_match("/__\w__/", $string, $result);
print_r($result);
\w is a single word character. It would match __t__ but not multiple characters like __test__. Try \w+

Regex Replace pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need the regular expression to replace using following rule:
Any integer present before . then no take place.
If no integer present before . then that should be replace.
$input = (contain anything characters, symbols, numbers, floats etc)
Example:
$myString = "Example 1.58 Stack.68";
Output should be
Example 1.58 Stack,68
preg_replace('#(?<!\d)\.#',',',$myString)

Categories