How to replace (.) dots by (·) interpuncts in PHP? - php

There's a French typographic rule to write properly some words in a genderless way, by adding an (·) interpunct between letters.
A few authors on my website are however typing a simple (.) dot instead.
As a solution, I'd like to create a function to replace in PHP strings each dots which are placed between two lowercase letters by interpuncts. But my PHP skills are rather limited… Here is what I'm looking for:
REPLACE THIS:
$string = "T.N.T.: Chargé.e des livreur.se.s."
BY THIS:
$string = "T.N.T.: Chargé·e des livreur·se·s."
Could someone help me please?
Thank you.

Use the preg_replace with pattern to dynamically match 3 groups - two lowercase letters (including special French letters) and dot between, and use the first and third captured group in replacement, along with intepunct:
$string = "T.N.T.: Chargé.e des livreur.se.s.";
$pattern = '/([a-zàâçéèêëîïôûùüÿñæœ])(\.)([a-zàâçéèêëîïôûùüÿñæœ])/';
$replacement = '$1·$3'; //captured first and third group, and interpunct in the middle
//results in "T.N.T.: Chargé·e des livreur·se·s."
$string_replaced = preg_replace($pattern, $replacement, $string);
More about preg_replace:
https://www.php.net/manual/en/function.preg-replace.php

You could use str_replace() if you know the grammar rules surrounding the dots you want to replace. (for instance, if everything between éand e is concerned, then you can do :
$bodytag = str_replace("é.e", "é·e", $sourceText);
But you will always risk some side effects. For instance if there is an acronym you don't want to be replaced with this pattern. I don't think there is any magic way to avoid this.
More specifically
I'd like to create a function to replace in PHP strings each dots which are placed between two lowercase letters by interpuncts.
This can be achieved with preg_replace() and the appropriate REGEX
See this post

Related

using regex for filtering some words in persian in php

I'm working on a script that is going to identify offensive words from text messages. The problem is that sometimes users make some changes in words and make them unidentifiable. my code has to be able to identify those too as far as possible.
First of all I replace all non-alnum chars to spaces.
And then:
I've written two regex patterns.
One to remove repeating characters from string.
for Example: the user has written: seeeeex, it replaces it with sex:
preg_replace('/(.)\1+/', '$1', $text)
this regex works fine for English words but not in Farsi words which is my case.
for example if you write:
امیییییییییین
it does nothing with it.
I also tried
mb_ereg_replace
But it didn't work either.
My other regex is to remove spaces around all one-letter words.
for example: I want it to convert S E X to sex:
preg_replace('/( [a-zA-Zآ-ی] )\1+/', trim('$1'), $text);
This regex doesn't work at all and needs to be corrected.
Thank you for your help
Working with multi-byte characters, you should enable Unicode Aware modifier to change behavior of tokens in order to match right thing. In your first case it should be:
/(.)\1+/u
In your second regex, however, I see both syntax and semantic errors which you would change it to:
/\b(\pL)\s+/u
PHP:
preg_replace('/\b(\pL)\s+/u', '$1', $text);
Putting all together:
$text = 'سسس ککک سسس';
echo preg_replace(['/(.)\1+/u', '/\b(\pL)\s+/u'], '$1', $text); // خروجی میدهد: سکس
Live demo

preg_match and preg_replace in php

I have a task to find and replace words starting and ending with "#".
Example - my string will look like:
Put your hands up in the air for #performer1# , Put your hands up in the air for #event#.
What I expect as a output is:
Put your hands up in the air for #performer1# , Put your hands up in the air for #event#.
I have no idea about regular expressions in php, and I'm a beginner, can someone help?
As you already suggested, the preg_replace function should do the trick. What you now need is a regular expression like this
$string = "Put your hands up in the air for #performer#, ...";
$pattern = "/#(\w+)#/";
$replacement = '<strong>$1</strong>';
$new_string = preg_replace($pattern, $replacement, $string);
The magic bit is the $pattern variable where you specify what to look for. If you put parenthesis around something, you can reference the actual contents in the $replacement variable.
The \w+ basically says: match as many characters as possible (and at least one) that are either a-z, A-Z, 0-9 or _.
The PHP PCRE Pattern Syntax can give you some more hints about how to use regular expressions.

Replace from one custom string to another custom string

How can I replace a string starting with 'a' and ending with 'z'?
basically I want to be able to do the same thing as str_replace but be indifferent to the values in between two strings in a 'haystack'.
Is there a built in function for this? If not, how would i go about efficiently making a function that accomplishes it?
That can be done with Regular Expression (RegEx for short).
Here is a simple example:
$string = 'coolAfrackZInLife';
$replacement = 'Stuff';
$result = preg_replace('/A.*Z/', $replacement, $string);
echo $result;
The above example will return coolStuffInLife
A little explanation on the givven RegEx /A.*Z/:
- The slashes indicate the beginning and end of the Regex;
- A and Z are the start and end characters between which you need to replace;
- . matches any single charecter
- * Zero or more of the given character (in our case - all of them)
- You can optionally want to use + instead of * which will match only if there is something in between
Take a look at Rubular.com for a simple way to test your RegExs. It also provides short RegEx reference
$string = "I really want to replace aFGHJKz with booo";
$new_string = preg_replace('/a[a-zA-z]+z/', 'boo', $string);
echo $new_string;
Be wary of the regex, are you wanting to find the first z or last z? Is it only letters that can be between? Alphanumeric? There are various scenarios you'd need to explain before I could expand on the regex.
use preg_replace so you can use regex patterns.

How can I add a space in string at capital letters, but keep continuous capitals together using PHP and a Regex?

I want to add a space to a string on capital letters using a PHP method like preg_replace() and a regex, but I only want to add a space on the first capital letter when they are continuous. I also would like the regex to know that the last capital in a continuous string of capitals should be the next capital to put a space before.
These strings are examples:
TodayILiveInTheUSAWithSimon
USAToday
IAmSOOOBored
become:
Today I Live In The USA With Simon
USA Today
I Am SOOO Bored
Can this be done, and how?
This question ( Regular expression, split string by capital letter but ignore TLA ), seems to accomplish this with .net.
WORKING SOLUTION
Here is the complete code that I use:
$string = 'TodayILiveInTheUSAWithSimon';
$regex = '/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/';
$string = preg_replace( $regex, ' $1', $string );
Both of these regex's work:
/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/
/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/
The first one is from #Regexident's solution below, and is very very slightly faster than the second one.
Find:
(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))
Replace:
$1
note the space before $1
Edit: fix.

Replacing a string using preg_match

I'm having trouble using preg_match to find and replace a string. The string of interest is:
<span style="font-size:0.6em">EXPIRATION DATE: 04/30/2011</span>
I need to target and replace the date, "04/30/2011" with a different date. Can someone throw me a bone a give me the regular expression to match this pattern using preg_match in PHP? I also need it to match in such a way that it only replaces up to the first closing span and not closing span tags later in the code, e.g.:
<span style="font-size:0.6em">EXPIRATION DATE: 04/30/2011</span><span class="hello"></span>
I'm not versed in regex, and although I've spent the last hour trying to learn enough to make this work, I'm utterly failing. Thanks so much!
EDIT: As you can see this has gotten me exhausted. I did mean preg_replace, not preg_match.
If you're after a replacement, consider using preg_replace(), something like
preg_replace('#(\d{2})/(\d{2})/(\d{4})#', '<new date>', $string);
How about this:
$toBeFoundPattern = '/([0-9][0-9])\/([0-9][0-9])\/([0-9][0-9][0-9][0-9])/';
$toBeReplacedPattern = '$2.$1.$3';
$inString = '<span style="font-size:0.6em">EXPIRATION DATE: 04/30/2011</span>';
// Will convert from US date format 04/30/2011 to european format 30.04.2011
echo preg_replace( $toBeFoundPattern, $toBeReplacedPattern, $inString );
and prints
EXPIRATION DATE: 30.04.2011
Patterns always begin and end with identical so called delimiter characters. Often the character / is used.
$1 references the string, which matched the first string matched by ([0-9][0-9]), $2 references be (...) and $3 the four letters matched by the last (...).
[...] matched a single character, which is one of those listed inside the brackets. E.g. [a-z] matches all lower case letters.
To use the special meaning character / inside of a pattern, you need to escape it by \ to make it be the literal slash character.
Update: Using {..} as pointed out below is shorthand for repeated patterns.
Regex should be:
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
If you want to only match one instance, this is OK. For multiple instances, use preg_match_all instead. Taken from http://www.regular-expressions.info/regexbuddy/datemmddyyyy.html.
Edit: are you looking to just search and replace inside a PHP script or do you want to do some javascript live replacement?

Categories