Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php using preg_replace to replace dot

Tags:

php

I want to extend my current preg_replace code and also replace all dots (.).

How can I extend my code, everything I try does not work.

<?php $optiontitleok = preg_replace('/(\s|&(amp;)?)+/', '', $optiontitle);?>
like image 201
JGeer Avatar asked Feb 27 '26 17:02

JGeer


1 Answers

You are searching for whitespace-characters OR ampersands, the below pattern also includes a check for periods (whitespace OR ampersands OR dots):

<?php $optiontitleok = preg_replace('/(\s|&(amp;)?|\.)+/', '', $optiontitle);?>

When in doubt building your regex, this site is a goldmine:

https://regex101.com

Hope this helps!

EDIT

As I stated in the comments I don't believe you are testing for and replacing the "+" signs. You're using it as a quantyfier at the end of your group. To replace whitespace, ampersands, plus-signs and periods, you could use the following pattern:

\s|\&|\.|\+

Result:

<?php $optiontitleok = preg_replace('/\s|\&|\.|\+/', '', $optiontitle);?>
like image 164
Robin Avatar answered Mar 02 '26 07:03

Robin