Most voted "preg-replace" questions
preg_replace() is a PHP function that uses regular expressions to make string substitutions.
Learn more…33 questions
Sort by count of
-
9
votes3
answers156
viewsRegular expression to reorder string
I have the result of a query with several joins, and in a row I have the concatenation of 2 groups. The line returns me a string as: 1,4|a1,b4. What I need is to regroup ID and Valor as follows:…
-
7
votes2
answers175
viewsRemove HTML snippet between specific comments
I have a class called Page(page.class.php) that "mounts" the page, and one of the functions of that class is to censor content by user level. <?php class Page(){ //(...) static function…
-
6
votes1
answer181
viewsRegex using preg_replace to separate values according to ")"
I need to use a string separation for the occurrences of closing of parentheses and a space after, with the following technique I managed to do: preg_replace('/[\)][\s]/', '$0--> $1', '1 () 2 ()…
-
4
votes1
answer96
viewsProblem with Regular Expression
preg_match('<[ \w"=]+name="xhpc_composerid"[ \w="]+>',$exec,$result1); echo $result1[0]; //input type="hidden" autocomplete="off" name="xhpc_composerid" value="u_0_1k"…
-
4
votes2
answers960
viewsPreg_replace for Preg_replace_callback
I’m having trouble migrating one script mine that clears characters from a sentence. The preg_replace (depreciated) I use the value and key of the array for the exchange, however the…
-
4
votes1
answer104
viewsProblems with PHP regular expressions?
I’m getting a file .txt and excluding the letters and lines in white. It is giving problem with the special character \t or \s he doesn’t recognize. The code below: <?php function pass1() {…
-
4
votes1
answer181
viewsHow to replace each occurrence of a regular expression match with a different value?
I have a text (html code) with several images in standard HTML format: <img src="X" atributos /> I need the value of the attribute src is replaced by the identifier CID:# where # is a unique…
-
3
votes1
answer393
viewspreg_replace PHP - / or @ why put?
Using preg_replace() in PHP, I came across the following situation: echo preg_replace('/:([\w]+)/', 'batata', ':quiabo/isso/:nada'); echo preg_replace('@:([\w]+)@', 'batata', ':quiabo/isso/:nada');…
-
3
votes2
answers374
viewsWhat are the valid delimiters for preg_regular expressions?
What are the valid delimiters for regular expressions in function preg_*? I know we can use some of these (/, ~ and #), as shown in the example below: $numero = '0.12.13'; preg_replace('/\D+/', '',…
-
2
votes1
answer530
viewsDetect if link is from the current or external domain with PHP
I’m currently using this expression with preg_replace to detect links in a content sent via POST: $conteudo = $_POST["conteudo"]; $conteudo =…
-
2
votes2
answers600
viewsHighlight term searched in bold with or without accent
Hello friends would love your help a lot. I am trying to highlight the words typed in bold in the search. the code below works only if you type "Brazilian selection" more if you type "Soccer…
-
2
votes3
answers112
viewsswap Z for S char between vowels in a string
How can I exchange all occurrences of the letter Z between S within a string for example TEREZA , CEZAR, BRAZIL, HOOK would be TERESA , CESAR, BRASIL, ANZOL…
-
2
votes2
answers3961
viewsHow to do a preg_replace with various conditions and changes?
How to make a PREG_REPLACE having several search and several substitutions at the same time For example I want ('/(BR|BL)/', 'B') if you have BR and BL replace with B and also ('/PH/', 'F') if PH is…
-
2
votes1
answer159
viewsRegex - Very high replace process time
Recently after developing a process, I saw that this was taking exorbitant 5~6min to be executed, something that should take at most 2s, so I started debugging the code with timers to know what…
-
2
votes2
answers110
viewsTreat all hexadecimal colors in a.css file using php and regex
I don’t know if you can help me but I want to replace the color in the "#xxxxxx" format in a CSS via Regular expression and PHP. I have a function that calculates the color and replaces it with the…
-
2
votes2
answers183
viewsProblem with Regex in PHP
I have the following function that is used to take special characters from a string: function removeSpecialChars($string){ //List (Array) of special chars $pattern =…
-
1
votes1
answer86
viewsLink being overwritten incorrectly when using preg_replace
I have a regular expression array that I use to replace url’s/hashtags in links using preg_replace: $regs = array('!(\s|^)((https?://|www\.)+[a-z0-9_./?=;&#-]+)!i', '/#(\w+)/'); $subs = array('…
-
1
votes0
answers63
viewsphp error to get data from a particular area of a site
Good evening I have a code in php to get the data of a given page, I use the initial template and it works I’m trying to use it in another site like this ai in the template but gives an error:…
-
1
votes2
answers236
viewsThe preg_replace is cutting letters with accent
$slug = preg_replace('/[^a-z0-9]+/i', '-', trim(strtolower($_POST["titulo"]))); Example: I send: Esse é um titulo de uma página He returns: esse-um-titulo-de-uma-p-gina As you can see, you’re…
-
1
votes2
answers69
viewsPreg Replace to remove awesome font from the texts
I need to ride a preg_replace PHP to remove all font-awesome of a string. for example.: Texto de teste <i class="fab fa-accusoft"></i> Teste Lorem Ipsim I’d have to change to Texto de…
-
1
votes0
answers41
viewsReplace, tag with attributes for merging documents
Hello I am creating a document that makes it possible to merge through tags, but, I will allow the user to add a separation within the tag, because I am doing this, in case of a customer…
-
1
votes1
answer93
viewsRegex separates by type of occurrences, how to do and the simple way to achieve?
Hello, I am working with user-agent and would like a help. I want to separate a value from a string that has this pattern: Mozilla/<version> (<system-information>) <platform>…
-
1
votes2
answers1248
viewsExpression to treat URL with parameter
NOTIFYING The solutions below, only will not have great serventias, with them it is necessary to have previously already implemented URL friendly, and also checks of display of the contents of the…
-
1
votes1
answer613
viewspreg_replace_callback() to remove characters that are not digits
I use Laravel and am trying to use the function preg_replace_callback() to remove point, comma, hyphen, dash and bar from the CNPJ numbers entered in the system. I used the function…
-
0
votes1
answer75
viewsProblem replacing accented characters
The function preg_replace is not behaving the way I predicted. By executing the code $string = preg_replace("/[ÁÀÂÃÄáàâãä]/", "a", $string); what I get back is a string filled with "a"s. If the…
-
0
votes1
answer112
viewsThe /e Modifier is deprecated
I’m making that mistake in generating a report I made here, is if anyone can help me. PHP Error was encountered Severity: 8192 Message: preg_replace(): The /e Modifier is deprecated, use…
-
0
votes1
answer546
viewsPHP: Character substitution within a string a parti of a certain position
I’m in need of a PHP way to replace parts of strings from a given position, for example: String: aaaaaaaaaa Replace from the 3º character by "-" Result: aaa------- or String: aaaaaaaaaa Replace…
-
0
votes0
answers24
viewsGet the value $1 of preg_replace inside an array that has a function execution inside
Well, I want to change a text from one sentence to another (it’s like an encryption) code being used: $dd = array(); $UserInfo = new UserInfo; $search = array( '/log\((.*)\);/' ); $replace = array(…
-
0
votes2
answers182
viewsFormat "hour:minute" duration to ISO 8601 format
I’m trying to turn the string 05:25 for PT05M25S. I made it this far, change the : for M. But I couldn’t insert the rest that was missing. $date = "05:25"; $date = preg_replace('/[:\/]/', 'M',…
-
0
votes1
answer61
viewsEregi_replace for preg_replace
Regular expression is not my strong suit in PHP and I need your help. I need to transform a string that comes from an apt post to be part of a url. Example: "Caio didn’t take a bucket of water" for…
-
0
votes1
answer58
viewsHow to Backreferencing with Regex in PHP?
Backreferencing to use the same text again in Pattern and leave another group in substitution only. It may have been confusing, but what I want is this. The String is this: $Str = ' <p>Os…
-
-1
votes1
answer191
viewsSwap the letters of the beginning of an email with another character
I’m trying to replace letters with asterisks of an email using the preg_replace() php, but php takes all the letters in the email, but I just want to put the asterisks in the first letters. example:…
-
-4
votes2
answers139
viewsRemove special characters while maintaining accents
I have this function below: <?php $titulo = "Notícia Com Ácêntös"; $titulo_novo = preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), $titulo); echo $titulo_novo; //…