Remove first instance of substring if its infront of string - php

I have 2 types of strings:
String 1
<br/>Ask Me A Question<br />
|<br/>Search My Apartments<br/>
String 2
Ask Me A Question<br />
|<br/>Search My Apartments<br/>
How do I have a function that remove the first <br/> from the String 1 to get String 2, while not touching anything in String 2 if String 2 is passed into the function?

start your regex with ^ to match the beginning of the string.
preg_replace('/^<br\s?\/>/', '', $string)
EDIT: whoops, had an extra space (\s) in there!
EDIT 2: added an optional space back in!

If you want to avoid using a regular expression you can check if <br/> occurs at the start of the string with strpos(). If so, simply lop off the first five characters using substr()
if (strpos($string, '<br/>') === 0) {
$string = substr($string, 5);
}

Replace `^<br/> with the empty string.

#mathletics Answer is not correct for the example given in the question, This one works:
preg_replace('#^(<br/>)#iu', '', $string);

Related

php preg_replace using $ from the end

I am trying to hip of from the end dot or comma and zero's using php
String is €69,00
Code is return preg_replace('/[\.,]{1}0*$/', '', $_price);
Search from right using dollar; find multipl zero's until you find the first comma or dot (from the right). Replace it with empty string.
Original request: show a formatted price that is integer, as a floor value without decimals.
String 1 in €69,00 and out €69
String 2 in €69,80 and out €69,80
String 3 in €69,95 and out €69,95
Somehow this is not working
question: How can I fix the regex to make integer whole values show without the decimals when the string is already formatted (not a number)
Another variation would be to look only for ,00:
(€\d+)(?:[,.]00)
... and replace this with $1, see a demo on regex101.com.
Which in PHP would be:
<?php
$string = <<<DATA
String 1 in €69,00 and out €69
String 2 in €69,80 and out €69,80
String 3 in €69,95 and out €69,95
DATA;
$regex = '~(€\d+)(?:[,.]00)~';
$string = preg_replace($regex, "$1", $string);
echo $string;
?>
try this one
preg_replace('/([€$])(\d+)([.,])(\d+)/', '$1$2', $_price);

Add + before word, see all between quotes as one word

I have a question. I need to add a + before every word and see all between quotes as one word.
A have this code
preg_replace("/\w+/", '+\0', $string);
which results in this
+test +demo "+bla +bla2"
But I need
+test +demo +"bla bla2"
Can someone help me :)
And is it possible to not add a + if there is already one? So you don't get ++test
Thanks!
Maybe you can use this regex:
$string = '+test demo between "double quotes" and between \'single quotes\' test';
$result = preg_replace('/\b(?<!\+)\w+|["|\'].+?["|\']/', '+$0', $string);
var_dump($result);
// which will result in:
string '+test +demo +between +"double quotes" +and +between +'single quotes' +test' (length=74)
I've used a 'negative lookbehind' to check for the '+'.
Regex lookahead, lookbehind and atomic groups
I can't test this but could you try it and let me know how it goes?
First the regex: choose from either, a series of letters which may or may not be preceded by a '+', or, a quotation, followed by any number of letters or spaces, which may be preceded by a '+' followed by a quotation.
I would hope this matches all your examples.
We then get all the matches of the regex in your string, store them in the variable "$matches" which is an array. We then loop through this array testing if there is a '+' as the first character. If there is, do nothing, otherwise add one.
We then implode the array into a string, separating the elements by a space.
Note: I believe $matches in created when given as a parameter to preg_match.
$regex = '/[((\+)?[a-zA-z]+)(\"(\+)?[a-zA-Z ]+\")]/';
preg_match($regex, $string, $matches);
foreach($matches as $match)
{
if(substr($match, 0, 1) != "+") $match = "+" + $match;
}
$result = implode($matches, " ");

Find last character of php string

I Have one string like below.
$string = "2346#$ABSC$%###234567";
Now I want last character from this string that is not numeric or special character, It should be only A-a to Z-z.
Means, I need only "C" from this string.
I have try this formula:
substr($string, -1);
You should look into regular expressions using something like preg_match()
An expression like this would match:
/([a-z])[^a-z]*$/i
It means:
([a-z]) Capture an a-z character (the i at the end makes it case-insensitive)
[^a-z]*$ followed by 0 or more non a-z characters until the end of the string
See an example.
This should work for you:
(Here I just replace everything expect a-zA-Z with an empty string. After this I just access the last character)
<?php
$string = '2346#$ABSC$%###234567';
$string = preg_replace("/[^a-zA-Z]/", "", $string);
echo $string[strlen($string)-1];
?>
output:
C
The proper regex is: ([a-z])[^a-z]*$

preg_replace only first number in string

First of all: I'm very bad with regular expressions, the ones I already have here I found on the net.
I want to increment the first number in a string, e.g. $teststring = "abcd1234efgh56";
I have already found the following:
preg_replace( "|(\d+)|e", "$1+1", $teststring);
will result in abcd1235efgh57
preg_replace( "|(\d+)(?!.*\d)|e", "$1+1", $teststring);
will result in abcd1234efgh57
but how do I increment ONLY the first number (1234 in the example) while leaving the rest of the string as is?
Thank you very much in advance
check out the docs: php.net manual on preg_replace
use the fourth parameter to limit the number of replaces
You could use:
preg_replace('/([^0-9]*)(\d+)(.*)/e', '"$1" . ($2+2) . "$3"' , $str, 1);
This would match all non-numeric characters (if any) before the first number as $1, the first number as $2, and all following characters (if any) as $3.
It then outputs the string with 2 added to the first number (you can change $2+2 to $2+NUMBER).
The final parameter 1 in the function is limiting the repeats to 1 time only...
The optional fourth parameter of preg_replace is limit :
preg_replace($search , $replace, $subject, $limit)
You can put 1 for your case..
it will give you
$teststring = 'abcd1235efgh56'
Try
preg_replace('/^([^0-9]+)?([0-9]+)([^0-9]+)?/', "$2+1", $teststring);

php expressions preg_match

I have been trying to figure this out really hard and I cannot came out with a solution ,
I have an arrary of strings which is
"Descripcion 1","Description 2"
and I need to filter by numbers, so I thought maybe I can use preg_match() and find when there is exactly 1 number 1 or two or etc, and do my logic, becouse the string before the number may change, but the number cannot, I have tried using
preg_match(" 1{1}","Description 1")
which is suppossed to return true when finds an space followed by the string "1" exactly one time but returns false.
Maybe some of you have had more experience with regular expressions in php and can help me.
Thank you very much in advance.
You could use strpos instead of preg_match!
foreach($array as $string) {
if(strpos($string, ' 1') !== false) {
//String contains " 1"!!
}
}
This would be much faster then a regular expression.
Or, if the Number has to be at the end of the string:
foreach($array as $string) {
if(substr($string, -2) == ' 1') {
//String ends with " 1"!!
}
}
You forgot the regex delimiters. Use preg_match('/ 1/', ...) instead.
However, you do not need a regex at all if you just want to test if a string is contained within another string! See Lars Ebert's answer.
You might have success using
if (preg_match('/(.*\s[1])/', $var, $array)) {
$descrip = $array[1];
} else {
$descrip = "";
}
I tested the above regex on the 3 separate string values of descripcion 1, thisIsAnother 1, andOneMore 1. Each were found to be true by the expression and were saved into group 1.
The explanation of the regex code is:
() Match the regular expression between the parentheses and capture the match into backreference number 1.
.* Match any single character that is not a line break character between zero and as many times possible (greedy)
\s Match a single whitespace character (space, tab, line break)
[1] Match the character 1

Categories