I'm trying to remove part of string in my php script. The strings will be similar to this one:
Samsung I8730 Galaxy Express
I need to remove part "I8730", this will be used on other models like "i9500", "B2100", etc. etc.
Please assist with some preg_replace pattern or something that will fix this problem.
Thanks.
A letter is [A-Za-z]. A number is \d.
/[A-Za-z]\d{3,4}/
First explode the string using on " ".
And then use preg_match() for the following regex.
^[a-zA-Z][0-9]{4}$
Use a regex and replace by nothing, this one matches if string has at least 1 char and 1 number:
$string = preg_replace('/[A-Za-z]+[0-9]+/', '', $string);
assume that after character there can be 2-5 intergers
$str = 'Samsung I8730 Galaxy Express';
echo preg_replace( '/([a-zA-Z][0-9]{2,5} )/','', $str);
This might helpful
Related
I have to replace string but it's simple in PHP but my string just like these here i show you.Please any one help me.
$string = "#x93F;#x902;#x91C";
Above string i want to replace it with
#x91C;#x93F;#x902;
But one thing in these string replace. We don't know last word of the $string #x91C; .
Any word comes in last it's place in to front of that string. How can i solve that please any one help me.
Use capturing groups to capture the characters you want. Later you could replace the matched characters with the chars inside the group.
Regex:
^([^;]*);([^;]*);([^;]*);$
Replacement string:
$3;$1;$2;
DEMO
$string = "#x93F;#x902;#x91C;";
echo preg_replace('~^([^;]*);([^;]*);([^;]*);$~', '$3;$1;$2;', $string);
Output:
#x91C;#x93F;#x902;
((?:[^;]+;)*)([^;]+)(?=$)
Replace by $2;$1.
See demo.
http://regex101.com/r/uH3tP3/9
I want to add a space to a string on capital letters using a PHP method like preg_replace() and a regex, but I only want to add a space on the first capital letter when they are continuous. I also would like the regex to know that the last capital in a continuous string of capitals should be the next capital to put a space before.
These strings are examples:
TodayILiveInTheUSAWithSimon
USAToday
IAmSOOOBored
become:
Today I Live In The USA With Simon
USA Today
I Am SOOO Bored
Can this be done, and how?
This question ( Regular expression, split string by capital letter but ignore TLA ), seems to accomplish this with .net.
WORKING SOLUTION
Here is the complete code that I use:
$string = 'TodayILiveInTheUSAWithSimon';
$regex = '/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/';
$string = preg_replace( $regex, ' $1', $string );
Both of these regex's work:
/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/
/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/
The first one is from #Regexident's solution below, and is very very slightly faster than the second one.
Find:
(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))
Replace:
$1
note the space before $1
Edit: fix.
I am trying to replace one or two letters in a string. Please consider this regex
$str = 'I haven\'t got much time to spend!';
echo preg_replace('/\b([a-z0-9]{1,2})\b/i','',$str);
returns: haven' got much time spend!
expected output: haven't got much time spend!
My goal is remove any one or two characters length words from a string. This can be alphanumeric or special characters.
Use lookarounds:
preg_replace('/(?<!\S)\S{1,2}(?!\S)/', '', $str)
Altho this leaves double whitespace when words are removed. To also remove spaces you could try something like:
preg_replace('/\s+\S{1,2}(?!\S)|(?<!\S)\S{1,2}\s+/', '', $str)
Just use:
echo preg_replace('/(?<!\S)\S{1,2}(?!\S)/i', '', 'a dljlj-b2 adl xy zq a');
The output is as wanted:
dljlj-b2 adl
So don't forget to handle beginning/end of a string by negative assertions.
I am having problems with RegEx in PHP and can't seem to find the answer.
I have a string, which is 3 letters, all caps ie COS.
the letters will change but always be 3 chars long and in caps, it will also be in the center of another string, surrounded by commas.
I need a regEx to find 3 caps letter inside a string and cahnge them from COS to 'COS'
(im doing this to amend a sql insert string)
I can't seem to find the regEx unless i use spercifit letter but the letters will change.
I need something along the lines of
[A-z]{3} then replace with '[A-Z]' (I know this isnt anywere near correct, just shorthand)
Anyone any suggestions?
Cheers
EDIT:
Just wanted to add incase anyone comes accross this question at a later date:
the sql insert string (provided from an external source and ftp's to my server daily)
contained the 3 capital string twice, once with commas and once with out
so I had to also remove the double commas added from the first regEx
$sqlString = preg_replace('/([A-Z]{3})/', "'$1'", $isqlString);
$sqlString = preg_replace('/\'\'([A-Z]{3})\'\'/', "'$1'", $sqlStringt);
Thanks everyone
You were actually very close. You could use:
echo preg_replace('/([A-Z]{3})/', "'$1'", 'COS'); //will output 'COS'
For MySQL statements I would advise to use the function mysql_real_escape_string() though.
$string = preg_replace('/([A-Z]{3})/', "'$1'", $string);
http://php.net/manual/en/function.preg-replace.php
Assuming it's like you said, "three capital letters surrounded by commas, e.g.
Foo bar,COS,Foo Bar
You can use look-ahead and look-behinds and find the letters:
(?<=,)([A-Z]{3})(?=,)
Then a simple replace to surround with single quotes will be adequate:
'$1'
All together, Here's it working.
preg_replace('/(^|\b)([A-Z]{3})(\b|$)/', "'${2}'", $string);
In PHP the trim function has a parameter for trimming specific characters (handy for leading zeros and the like). I can't seem to get it to accept a vertical bar (|) character. Anyone know how to get this working? I tried the hex value but had no luck. I'm sure it's something simple.
Cheers
It works for me:
var_dump(trim('|foo|', '|')); // string 'foo' (length=3)
Maybe you have some whitespace around it, or your're using the wrong pipe character? ¦ vs |
Works for me:
$str = "|test string";
echo trim($str, "|");
test string
Can you show some of your code?
Maybe you want to remove a | in the middle of a string
you can use str_replace
str_replace("|", "", $str);
echo trim('|text|', '|'); // returns text
The second param was added in PHP 4.1!
trim() only removes characters from the beginning and end of a string. If you'd like to replace characters in the middle of a string, use str_replace(), or preg_replace() if you like regular expressions.