I have php code that writes a string, which is actually an html file, to the server, but before the write I want to rip through and replace all "Npx" with "[N/10]rem". So "width:203px" would become "width:20.3rem" and top:46px" would become "top:4.6rem". Does anyone see a regex string that will do this?
Thanks
Just capture the digit before px then match the string px and replace all the chars with .$1rem
. Where $1 refers to the characters which are present inside the group index 1.
(\d)px
Replacement string:
.$1rem
DEMO
$string = <<<EOT
width:203px
top:46px
top:6px
EOT;
$pattern = "~(\d)px~";
$replacement = ".$1rem";
echo preg_replace($pattern, $replacement, $string);
Output:
width:20.3rem
top:4.6rem
top:.6rem
Related
Trying to construct a regex that will locate a pattern of ANY character followed by double quotes
This regex locates each occurrence properly
(\S"")
Given the example below
$string='"WEINSTEIN","ANTONIA \"TOBY"","STILES","HOOPER \"PETER"","HENDERSON",';
$pattern = '(\S"")';
$replacement = '\\""';
$result=preg_replace($pattern, $replacement, $string);
My result turns out to be
"WEINSTEIN","ANTONIA \"TOB\"","STILES","HOOPER \"PETE\"","HENDERSON"
But I am seeking
"WEINSTEIN","ANTONIA \"TOBY\"","STILES","HOOPER \"PETER\"","HENDERSON"
I understand the replacement is removing/replacing the whole match, but how can I remove all but the first letter rather than completely replacing it?
You can change your pattern to use a positive lookbehind instead so that it doesn't capture the non-space character:
$string='"WEINSTEIN","ANTONIA \"TOBY"","STILES","HOOPER \"PETER"","HENDERSON",';
$pattern = '/(?<=\S)""/';
$replacement = '\\""';
$result=preg_replace($pattern, $replacement, $string);
echo $result;
Output
"WEINSTEIN","ANTONIA \"TOBY\"","STILES","HOOPER \"PETER\"","HENDERSON",
Demo on 3v4l.org
Hi I'm new to php and I need a little help
I need to change the text that is between ** in php string and put it between html tag
$text = "this is an *example*";
But I really don't know how and i need help
personally I would use explode, you can then piece the sentence back together if the example appears in the middle of a sentence
<?php
$text = "this is an *example*";
$pieces = explode("*", $text);
echo $pieces[0];
?>
Edit:
Since you're looking for what basically amounts to custom BB Code use this
$text = "this is an *example*";
$find = '~[\*](.*?)[\*]~s';
$replace = '<span style="color: green">$1</span>';
echo preg_replace($find,$replace,$text);
You can add this to a function and have it parse any text that gets passed to it, you can also make the find and replace variables into arrays and add more codes to it
You really should use a DOM parser for things like this, but if you can guaratee it will always be the * character you can use some regex:
$text = "this is an *example*";
$regex = '/(?<=\*)(.*?)(?=\*)/';
$replacement = 'ostrich';
$new_text = preg_replace($regex, $replacement, $text);
echo $new_text;
Returns
this is an *ostrich*
Here is how the regex works:
Positive Lookbehind (?<=\*)
\* matches the character * literally (case sensitive)
1st Capturing Group (.*?)
.*? matches any character (except for line terminators)
*? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
Positive Lookahead (?=\*)
\* matches the character * literally (case sensitive)
This regex essentially starts and ends by looking at what is ahead of and behind the search character you specified and leaves those characters intact during the replacement with preg_replace().
I would like to remove all words from the end of a text that are starting with a space and # sign.
URLS or hashtags within a sentence should not be remove.
Example text:
hello world #dontremoveme foobar http://example.com/#dontremoveme #remove #removeme #removeüäüö
I tried this but it removes all hashtags:
$tweet = "hello world #dontremoveme foobar http://example.com/#dontremoveme #remove #removeme #removeüäüö";
preg_match_all("/(#\w+)/", $tweet, $matches);
var_dump( $matches );
My idea is to check every word starting at the end of the text for a leading # with a space in front, until it's no longer the case.
How to translate that into a regular expression?
You could use something like so: ( #[^# ]+?)+$ and replace it with an empty string.
An example is available here. Since you have non ASCII characters, the . operator (which matches any character) should help you tackle any character.
The following regex matches all words starting with a [Space]# at the end of the line.
/( #\S+)*$/g
https://regex101.com/r/eH4bJ2/1
This will do the job:
$tweet = "hello world #dontremoveme foobar http://example.com/#dontremoveme #remove #removeme #removeüäüö";
$res = preg_replace("/ #\p{L}+\b(?!\s+\p{L})/u", '', $tweet);
echo $res,"\n";
Output:
hello world #dontremoveme foobar http://example.com/#dontremoveme
$text = 'Hello #demo here!';
$pattern = '/#(.*?)[ ]/';
$replacement = '<strong>${1}</strong> ';
echo preg_replace($pattern, $replacement, $text);
This works, I get HTML like this: Hello <strong>demo</strong> here!. But this not works, when that #demo is at the end of string, example: $text = 'Hello #demo';. How can I change my pattern, so it will return same output whenever it is end of the string or not.
Question 2:
What if the string is like $text = 'Hello #demo!';, so it will not put ! as bolded text? Just catch space, end of string or not real-word.
Sorry for bad English, hope you know what I need.
In order to select a word beginning with the # symbol, this regex will work:
$pattern = "/#(\w+)\b/"
`\w` is a short hand character class for `[a-zA-Z0-9_]`. `\b` is an anchor for the beginning or end of a word, in this case the end. So the regex is saying: select something starting with an '#' followed by one or more word characters until the end of the word is reached.
Reference: http://www.regular-expressions.info/tutorial.
You could use a word boundary, that's what they're for:
$pattern = '/#(.+?)\b/';
This will work for question 2 also
You can add an option to match the end of the string:
#(.*?)(?= |\p{P}?$)
Replace with <strong>$1</strong>.
You can also use \p{P} (any Unicode punctuation symbol) to prevent punctuation from bold formatting.
Here is a demo.
I've got the following source:
<font color="black">0</font><font color="white">1101100001001101</font><font color="black">1</font><font color="white">0110</font>
And would like to replace all white 1 and 0 with spaces. I can match them easily with
/<font color="white">([10]*)</font>/g
Is there a replace pattern (I'm using PHP) to generate the same number of spaces for the matching group $1?
The result should look like this:
<font color="black">0</font><font color="white"> </font><font color="black">1</font><font color="white"> </font>
(And please ignore the fact that I'm parsing HTML with regexs here. I'm more interested in the solution to the regex problem than in the HTML.)
$test = '<font color="black">0</font><font color="white">1101100001001101</font><font color="black">1</font><font color="white">0110</font>';
echo preg_replace ('~(<font color="white">)([10]*)(</font>)~e', '"\\1" . str_repeat(" ", strlen ("\\2")) . "\\3"', $test);
Try this here
<?php
$string = '<font color="black">0</font><font color="white">1101100001001101</font><font color="black">1</font><font color="white">0110</font>';
$pattern = '/(?<=<font color="white">)( *?)[10](?=.*?<\/font>)/';
$replacement = '$1 ';
while (preg_match($pattern, $string)) {
$string = preg_replace($pattern, $replacement, $string);
}
echo $string;
I use a positive look behind (?<=<font color="white">) to search for the color part. And a positive look ahead (?=.*?<\/font>) for the end.
Then I match the already replaced spaces and put them into group 1 and then the [10].
Then I do a while loop until the pattern do not match anymore and a replace with the already replaces spaces and the new found space.
This regex will only match 1 | 0 if it's preceded with "white">.
the (?<=...) syntax in regex is called positive lookbehind...
(?<="white">)([10]+)