Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two questions, first

PHP
<?php
$text = "a iKo SaioT eeee";
$fonts = "a|i|u|e|o";
$newText = preg_replace("#([$fonts].+?(?=[^$fonts![:space:]]))#us", '<b>$1</b>', $text);
echo $newText;
?>


Will be like (a iKo SaioT eeee). Not (a iKo SaioT eeee). Why the last eeee letter does not change bold? And when $text = "a" The result does not turn into bold too


Second, how to convert this code into jquery

PHP
$newText = preg_replace("#([$fonts].+?(?=[^$fonts![:space:]]))#us", '<b>$1</b>', $text);


Cause $fonts is arrays not same in php

What I have tried:

I need without space too.
Because if I put space in array a|i|u|e|o|[:space:].
It will be like this <b>e</b><b>e</b><b>e</b><b>e</b> will be separate. And if i using #([$fonts]?(?=[^$fonts![:space:]]))#us without .+, first and last, fonts not being bold

If i using
preg_replace('/['.$fonts.']/i', '<b>$1</b>', $text);
The result will be like this, right? a iKo SaioT eeee but if you click right and inspect.
You can see <b>a</b> <b>i</b>K<b>o</b> S<b>a</b><b>i</b><b>o</b>T <b>e</b><b>e</b><b>e</b><b>e</b>
Being separated by spaces

But different from using ^$fonts![:space:]
To be <b>a</b> <b>i</b>K<b>o</b> S<b>aio</b>T eeee

Why i using exeption space? Cause Arabic script is continuous
If using ^$fonts![:space:] to be like <b>مَنْ جَاءَ بِا</b>
If not <b>م</b><b>َ</b><b>ن</b><b>ْ</b> <b>ج</b><b>َ</b><b>ا</b><b>ء</b><b>َ</b> <b>ب</b><b>ِ</b><b>ا</b>
Not same مَ نْ جَ اءَ بِ ا = مَنْ جَاءَ بِا

But this problem, like my question in post
Why the last eeee letter does not change bold? And when $text = "a" The result does not turn into bold too.
Posted
Updated 21-Jul-17 23:41pm
v11
Comments
Patrice T 21-Jul-17 22:40pm    
What is supposed to do "#([$fonts].+?(?=[^$fonts![:space:]]))#us" ?
aritabacan 22-Jul-17 2:58am    
If i using
preg_replace('/['.$fonts.']/i', '<b>$1</b>', $text);
The result will be like this, right? a iKo SaioT eeee but if you click right and inspect.
You can see <b>a</b> <b>i</b>K<b>o</b> S<b>a</b><b>i</b><b>o</b>T <b>e</b><b>e</b><b>e</b><b>e</b>
Being separated by spaces

But different from using ^$fonts![:space:]
To be <b>a</b> <b>i</b>K<b>o</b> S<b>aio</b>T eeee

Why i using exeption space? Cause Arabic script is continuous
If using ^$fonts![:space:] to be like <b>مَنْ جَاءَ بِا</b>
If not <b>م</b><b>َ</b><b>ن</b><b>ْ</b> <b>ج</b><b>َ</b><b>ا</b><b>ء</b><b>َ</b> <b>ب</b><b>ِ</b><b>ا</b>
Not same مَ نْ جَ اءَ بِ ا = مَنْ جَاءَ بِا

But this problem, like my question in post
Why the last eeee letter does not change bold? And when $text = "a" The result does not turn into bold too.
Patrice T 22-Jul-17 2:58am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

Let simplify the code for PHP. The below will tell the code to replace a or i or u or e or o with bold font and ignore case sensitivity.

PHP
$newText = preg_replace('/['.$fonts.']/i', '<b>$0</b>', $text);

Here is the version for jQuery
replace characters with bold - JSFiddle[^]

Output:

a iKo SaioT eeee
 
Share this answer
 
v3
Comments
aritabacan 22-Jul-17 1:37am    
Yes, I understand. But the result will be different like mine, which uses exceptions.

[^$fonts![:space:]]

If i using u code, result like this <b>e</b><b>e</b><b>e</b><b>e</b>
I want to make it one to be <b>eeee</b>
So I have to use

$fonts![:space:]
Bryian Tan 22-Jul-17 15:56pm    
how about
$newText = preg_replace('/['.$fonts.']{1,}/i', '$0', $text);
? this will return <b>eeee</b> same with the jQuery replace characters with bold - JSFiddle[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900