5,832 questions
1
vote
3
answers
72
views
Remove more than 1 blank line between paragraphs with only 1 blank line AND remove excess white spaces
I have a website where users are required to write a simple, short profile description. Some users are writing ugly profiles with a bunch of empty spaces, and excess new lines. It's a lot of work to ...
-7
votes
1
answer
124
views
Replace the querystring of an href declaration in an <a> tag
I want to replace the following hyperlinks dynamically
from
<a href="/xsearch2?q=some search/21">21</a>
to
<a href="/xsearch2?q=some search&page=21">21</a&...
-1
votes
1
answer
96
views
How to use PHP preg_replace on a formatted number based on search query while ignoring comma and decimal?
I want to highlight my search query on a formatted number.
For example:
$search_query = '1234'; // or $search_query = '7800';
$formatted_numeber = '12,345,678.00';
Currently my code is:
preg_replace('...
0
votes
1
answer
72
views
PHP - replace text and check for file?
I have this simple line of php
$text = preg_replace("/\\ii(.*?)\\<>/", "<img scr='img/$1'>", $text);
Where I change II30.jpgII to <img scr='img/30.jpg'>, and it ...
1
vote
0
answers
90
views
Trying to match any set of elements with a regex that have or whitespace [duplicate]
So I am currently using this regex, but I see a flaw in it for p and div tags:
(<(p?|div?)>).+(\s| )+(<\/(p?|div?)>)+
Given the current string:
<div class="blahblahblah ...
0
votes
0
answers
36
views
Using preg_replace for bbcodes to convert texts
I'm in the process of upgrading my site's engine. The new version does not support bbcodes, and all my forum posts and articles are created using it.
I want to make a script to convert all text with ...
4
votes
3
answers
148
views
Converting this preg_replace to match a word with a plus
In PHP I need to highlight multiple given words in a string, for example wrapping found matches inside a <em> tag.
But if I have a word ending in + I cannot do it.
I understand the below problem ...
0
votes
0
answers
27
views
preg_replace: get keys from short tags [field id="key"] and replace them with values [duplicate]
I have to determine all short tags like [field id="key1"] in the string, get the keys from the short tags to use them to get the values like $fields["key1"], then replace in the ...
0
votes
1
answer
120
views
Replace only the first occurence of two special characters, the rest are removed and no numbers [closed]
I need a php preg_replace to allow only the first occurence of ' and -, the rest will be removed and no numbers and other special characters. So Mike O' Hara's Du-Pont Check-In will become Mike O' ...
0
votes
0
answers
73
views
Do I need preg_replace_callback() to replace all matches in a text? [duplicate]
As part of a migration, I wrote a function to transform links, prepending a path ("/my/files") to existing paths from the source site. Here is my function:
function example_file_urls($text) ...
0
votes
1
answer
75
views
Changing from file_get_contents to curl
I've been using file_get_contents in php until the last month when it's stopped working. So I've tried using curl but I'm now getting errors when I extract data from the downloaded webpage (finance....
0
votes
1
answer
66
views
Replace preg_replace with preg_replace_callback due to e modifier deprecation
Get a warning because the e modifier deprecation on preg_replace function.
Here is the code, I suggest that should be replaced by preg_replace_callback function (for php 7.4.16) :
`$text = ...
0
votes
2
answers
106
views
PHP perl regular expression - URL not preceded by equal sign and possible single or double quote
I'm trying to create a perl regular expression that matches a URL that is not preceded by an equal sign and one single or double quote (optional) ignoring whitespace. The code below gives an error: ...
0
votes
2
answers
52
views
Remove tag with a specific attr from text PHP
I have a span tag that has role attr button. What I want to achieve is to remove the whole span while matching not the span itself but the role attribute
$str = 'This is a test buton. <span id=&...
-1
votes
1
answer
106
views
Replacing quotes with single quotes between two strings in php using preg_replace [duplicate]
I'm looking for a preg_replace that can replace " with ' when between font-family: and ;
I'm having an issue with my some dynamic html I'm using which requires me to replace the quotes with ...
1
vote
1
answer
52
views
preg_replace for a specific domain name
I was using str_replace to rewrite URLs to PDFs from https://example.com/documents/en/whatever.PDF to https://example.com/documents/es/whatever_SPANISH.pdf
This is what I was using
if($_COOKIE['...
0
votes
0
answers
46
views
REGEX search only affecting last occurence in preg_replace function in PHP error [duplicate]
I have a function highlighting word for a glossary. I created a regex to replace occurrence of word that are preceded and followed by word starting/ending characters like spaces, commas, HTML tags, ...
0
votes
0
answers
71
views
Cannot remove whitespace from html file in order to preg split
We receive HTML blood files for clients and I am trying to finish some PHP code to strip, clean and preg strip the code so that I can assemble multiple files into a spreadsheet. The issue is that the ...
1
vote
2
answers
100
views
Replace corrupted alphanumeric id substring containing unwanted spaces
I have content that is coming dynamic but some characters from string are missing. I want to replace that characters to make it correct. I have complete order id but in content it's coming with space....
0
votes
1
answer
47
views
PHP preg_replace double quote not working
I loaded a csv to array.
And I want to put " to column 21 if they don't have yet. This is sample of column 21. Some are empty. It's $data[21] in my case.
21
14"
14"
14"
empty
16&...
0
votes
2
answers
81
views
Replace portion of url filepath found in HTML page text
Given some urls inside an html page, I want to replace some urls as below:
Example url: https://example.com/cost-center/sub-one/article1
From that one I want to replace the text that is between /cost-...
0
votes
2
answers
94
views
Convert html headlines to list elements in PHP
I am learning php language.
I want to show the table of contents for the article. Convert the headings (h2,h3,h4,...) into a list and create links.
This is my php code.
$Post = '
<h2>Title 01<...
0
votes
2
answers
86
views
shortcode preg_replace by php
I am learning the language php. I have two functions and I want to connect these two and show them in one output php.
<?php
function link_follow($id)
{
$title = '';
$slug = '';
if ( $id ...
0
votes
1
answer
52
views
With PHP/Preg, how is it possible to replace a string not followed by any character [duplicate]
For example, I have two strings
"abcdef"
"abcdefghijk"
I want to replace the first one, but not the second one which begins with the same string. However, the first one can be ...
0
votes
1
answer
88
views
Remove all Attribute from img except src and alt
I have a html string that contains many images.each image has some attribute like class ,id , style , ....
I want remove all attribute from img except src and alt.
before :
<img title="How to&...
-2
votes
1
answer
37
views
Preg_replace a specific strings first character
I have a string of "You can go to kitchen and get some food for your dinner" and i need to change with preg_replace words You, your, food case insensitive to <i>y</i>ou,<i>...
0
votes
2
answers
105
views
Add newlines to separate text and only hashtags that appear at the end of the string
I've already tried a couple of regex but cannot fix this one:
I need an enter line before the hashtags start (so there is some space).
Example:
It's essential to keep your pup cool in the summer ...
1
vote
3
answers
411
views
Remove the first 1 and space before a phone number?
I did searches and figured this would be an easy find. Though I haven't found anything related to exactly what I need and similar questions are from people from overseas that have different phone ...
-1
votes
2
answers
34
views
PHP: how to insert HTTP in <a href="nunm-press.com"> that doesn't have any?
I need to insert only a HTTP in a list of links (10,000) that doesn't have one, for exemple
<a href="nunm-press.com">
From
<a href="nunm-press.com"
to
<a href="...
-1
votes
1
answer
29
views
Ordered string to array PHP
I have a problem with converting the string:
$str = "1. List of word synonyms 2) Glass plastic products 3. Other product";
into an array list:
Array(
[0] => List of word synonyms
[1] ...
0
votes
2
answers
325
views
php PDO replace anonymous parameters ('?') in queries with redable values
I want a way to show SQL query how it will look with anonymous parameters (?) substituted by actual parameters.
This is only for readability purposes and debugging, and wouldn't be used as an actual ...
0
votes
1
answer
50
views
PHP preg_match can't replace with number properly
$pedit =<<<head
<div class="item"><div var="i_name_10">white gold</div> <div
var="i_price_10">$5.99</div></div>
head;
echo ...
1
vote
2
answers
54
views
Weird PHP preg_replace result
$pedit =<<<head
<div class="item"><div var="i_name_10">white gold</div> <div var="i_price_10">$5.99</div></div>
head;
$...
1
vote
1
answer
56
views
Regex (php) to match single [ or single ] but ignore anything between [[ ]]?
I have a string, and within it there will be combinations of [ ] [[ ]] ][ but I need to replace the single [ and ] with < and > but leave alone (don't match) anything that is between [[ ]].
I ...
1
vote
1
answer
106
views
Convert Apple Maps coordinates in universal format
I would like to convert the coordinates displayed in Apple Maps as
38,89016° N, 1,22427° O
(German notation)
or
38.89016° N, 1.22427° E
(English notation)
into something like
38.89016, 1.22427
(...
1
vote
2
answers
37
views
Unwanted replacements from multipass search-and-replace
I'm trying to use PHP to replace some item ids in a piece of JSON, and experiencing a problem where the replacements from the first pass are being overwritten by the replacements from the second pass.
...
1
vote
1
answer
77
views
preg_replace_callback of url in textarea cuts off at underscore or colon
Not sure how to proceed to allow or prevent snipping at underscore and colon in urls in this preg_replace. Also not sure what other special characters I might be missing that will snip the url
$text = ...
0
votes
1
answer
65
views
Seeking help to write a PHP preg_replace pattern to replace inconsistent values
In JSON string I need to replace inconsistent values in multiple instances
String format to replace within each instance
inconsistent value 252/2022/08 to be replaced with consistent value 178/2023/03
...
0
votes
1
answer
64
views
preg_replace reuse matched value in replacement value
I want to perform value replacement based on matched value. The replacement expression has calculation based on matched value.
regex
<?php
$re = '/<w:d([^\[]+)\/>/m';
$str = '<w:d2/>';...
-1
votes
2
answers
76
views
Problems using preg_replace
Regx is not my thing.
I have a large file where I want to replace the following example:
<g:id><![CDATA[131614-3XL]]></g:id>
should be replace with:
<g:id><![CDATA[131614-...
-2
votes
2
answers
137
views
PHP Regex Get Text Between and inside tags
I have this tag on an HTML code:
text html [button link="google.com" color="#fff" text="this text here"] rest of html
I wish i could have the parameters of this "...
0
votes
1
answer
222
views
Trying to Replace h2's with Anchor Links within the_content() dynamically
I am trying to create a side anchor navigation that will build out the side nav for every h2 tag header. I have the side nav portion working just the way I want it to. It pulls the h2 out of the ...
2
votes
2
answers
111
views
How to exclude image extension from string place in PHP
I want to replace all the string values from example.com to sample.com excluding the image extension .png, .jpeg, .jpg etc.
$body = ' www.example.com, example.com, example.com/hello.html, example.com/...
2
votes
4
answers
275
views
Replace all domain including subdomain name using PHP with Regex
I am using preg_replace or str_replace in PHP to replace all the domain name including www
$string = ' https://example.com, https://www.example.com, https://subdomain.example.com ';
$olddomain= "...
-2
votes
1
answer
54
views
Replace portion of css file content using preg_replace() [closed]
$settings['new_primary_colour_ccs'] = '#777777'; // test color
foreach($appPats as $path){
$styleSettings = [
'primary_colour' => [
'reg' => "\bprimaryColour:\s?['\&...
0
votes
1
answer
401
views
PHP preg_replace find a word in string and remove other characters
I am trying to remove from a string all characters that do not match to a list of words.
my list of words could be:
person
animal
a string can look like this:
123-ea-person.jpg
456456-on-Person.jpg
...
2
votes
2
answers
64
views
PHP: Regex to replace dash only if there are specific word and number before it
I am trying to replace a dash between two time values and replace it with the word to but only if before the first time value there is the word from
This is what I have until now, which works fine, ...
0
votes
1
answer
56
views
PHP: Replace number in a string with its word equivalent [duplicate]
I have a function that converts numbers into words called NumberToWords.
Then I have a sentence like this
$str = "There are 3 cats and 5 dogs".
NumberToWords(3) will read three
NumberToWords(...
0
votes
0
answers
41
views
Using regex to match multiple optional arguments in an enclosed custom function
I'm building a website and I have created a couple of custom functions that add certain information on the page. One of them is as follows:
{{i|Custom argument 1|Custom argument 2|Custom argument 3|......
1
vote
1
answer
61
views
Preg_replace string
I am creating a username from the users Billing First Name and Billing Last Name.
This in in. WooCommerce, though I suppose the cms is irrelevant to my question.
The following characters should be ...