I need to trim the last 7 digits in one field of my form
and I need to trim the first three digits in another form
Im assuming that I use ltrim and rtrim but how exactly do I write it? I think i need to use ltrim and rtrim but I don't know how to write the code
ltrim() and rtrim() will remove the spacing characters.
Slapyo is correct, substr() is probably a way of doing it. It may look something like:
<?php
$Var = "mystringofcharacters";
$Result = substr($Var,0,strlen($Var)-7);
echo $Result;
?>
Should return: mystringofcha
We use strlen() because of how you want to return the string, "all characters except the last X" means you need to know how long it is, then get all the other characters.
If you wanted only the last 7 characters, you'd replace the strlen($Var)-7 section with just -7.
Related
How does rtrim work? I have a string "4dbb3dca&". I am not sure how my string is formmated.
I want to call rtrim('4dbb3dca&', '&')
--edit:& might be $amp; special character issue
But after my string is 4dbb3dc.
I expect it to be 4dbb3dca.
Is there any workaround? I ried to use &, but then rtrim does nothing and initial string is returned. Is it possible to use rtrim in my situation? Does it depend on php version?
--- edit ---
after some research I realized that I want:
I want remove & using & in rtrim() function. Is this possible?
Q1. Is it possible to use rtrim in my situation?
Yes, it is possible to use rtrim() in your situation.
Q2. Does it depend on php version?
No
You just need to use echo rtrim('4dbb3dca&', '&'); not the & because 2nd parameter of rtrim works like below-
You can also specify the characters you want to strip, by means of
the character_mask parameter. Simply list all characters that you
want to be stripped
DEMO: https://3v4l.org/anFIR
If you use this below example, then you'll get the same result because it'll try to remove all the given character(s) from the right side of your given string. For Ex-
echo rtrim('4dbb3dca&;pm', '&');` // will also return 4dbb3dc
The second string you specify in rtim() contains all the extra characters you want to trim. So the chars getting trimmed are "&" and "a" (both are indeed included in the second parameter of your rtrim() function call, "&").
To get the result that you want, you should invoke rtrim() this way:
rtrim('4dbb3dca&', '&')
It looks like you're trying to remove an ampersand from the end of the string, potentially in encoded form. rtrim doesn't give you enough control to do that, as you've seen, but you can do it with preg_replace.
echo preg_replace('/&(amp;)?$/', '', $string);
The pattern will match an ampersand at the end of the string, optionally followed by amp;.
The way the rtrim works remove the last characters at the second argument.
the PHP has split the string to characters and make a check if you have the characters at last of the string.
if you have two characters from the list of characters the rtrim will remove both
For example :
;&apm it will be the same result like &
if you want to remove the only ampersand symbol at the end of the string
use this code: rtrim('4dbb3dca&','&');
for understand more you can read about it at php.net
http://php.net/manual/en/function.rtrim.php
I am trying to rtrim() the complete datetime whenever the time is 00:00:00.
In this scenario, I just want to display date without any time.
Code:
echo rtrim('26-10-2015 06:00:00',' 00:00:00');
Result:
26-10-2015 06
I want to match the complete string to apply the right trim.
The result you obtain is correct. The character_mask parameter help says:
You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
So, you strip the 0, : and a space from the string end.
If you still want to use your approach, use
echo rtrim(rtrim('26-10-2015 06:00:00','0..9:'));
The 0..9 "matches" all digits and the outer rtrim() will get rid of any trailing whitespace that remains after the custom rtrim.
See IDEONE demo
If you plan to change the approach, you may use strstr to obtain the substring before the first space like this:
echo strstr('26-10-2015 06:00:00',' ', true);
See demo
use date() and strtotime()
echo date("d-m-Y",strtotime('26-10-2015 06:00:00'));
I have some php string like below
abc-1987-mp3-songs
xyz-1999-india-mp3-songs
dec-2001-mp3-songs
ryu-2012-freemp3-songs
Now I want these string splited at last found numeric values like below
abc-1987
xyz-1999
dec-2001
ryu-2012
Please help me that which regex can be used to do this. thanks.
Ok, I had a look (do take some time to learn regex - but meanwhile):
$split = (preg_split('/(^.*?[0-9]+)\-?[^0-9]+/', 'foo-xyz-1999-india-mp3-songs', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
echo $split[0];//<--- foo-xyz-1999, just like you wanted
Dumps an array with foo-xyz-1999 as first value, which is what you need. If you want to know what every part of the regex does read it here
The only difference is that, though the whole string becomes its own delimiter, there are two delimiters (the first part, always ending on a series of numbers and the rest of the string, that doesn't contain any more digits)
Use explode insted of regular expression
for example:-
$str="abc-1987-mp3-songs";
$f=explode("-",$str);
echo $final_result=$f[0]."-".$f[1];
or if you want to use reg exp.then
<?php
$str="abc-1987-mp3-songs";
echo $f=preg_replace('/[^0-9]/','', $str);
?>
Above code give you all the numeric digits of your string.
This would match last occurrence of numeric value from given string:
([\w\d-]*-[\d]+)
This is the link: Regex
There is a string
http://www.ccdcdlmcc.sdc.smdc.,ms.cmcsjh?page=2$#$#L$JK#J$LK#J$
I want to replace the page number with a different string "[pageno]" (including square brackets),
so that my final string will be:
http://www.ccdcdlmcc.sdc.smdc.,ms.cmcsjh?page=[pageno]$#$#L$JK#J$LK#J$
I already tried it with explode and str_split($string), but no luck... can it be done by a regular expression?
My main Stringhttp://www.xyz.com/s/ref=sr_pg_2?rh=n%3A117332031%2Cn%3A!117333031%2Cn%3A118457031%2Cn%3A118458031&page=2&bbn=118457031&ie=UTF8&qid=1337146507 is
Here you go:
$str=preg_replace("/(\\?)(page)(=)(\\d+)/","?page=[pageno]",$yourstring);
Here's another way to do it:
<?php
$str = "http://www.ccdcdlmcc.sdc.smdc.,ms.cmcsjh?page=2$#$#L$JK#J$LK#J$";
echo preg_replace('/page=([0-9]+)/', 'page=[pageno]', $str);
?>
I do assume that the page number is a numeric value only. Otherwise you'll have to extend the [0-9] with [0-9a-zA-Z] and perhaps other characters you want to allow.
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);