Remove a string php - php

I have this value: samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f
Best approach to remove all the string from the beginning of the word until the |. The | is the key here , on where to end. The output should be d76c3c301eb754c62b981f7208158a9f.

Use explode
explode("|","samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f")[1];
check this : https://eval.in/591968

Use stristr()
$str="samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f";
echo ltrim(stristr($str, '|'),"|");

Another short solution using substr and strpos functions:
$str = "samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f";
$result = substr($str, strpos($str, "|") + 1); // contains "d76c3c301eb754c62b981f7208158a9f"

Split your string by using explode(). Then return the last element of the array using end()
end(explode('|', 'samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f'));

Try below code
substr( strstr('samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f', '|'), 1);

Use php explode($delimiter,$yourstring) function
$str="samplemail#yahoo.com|d76c3c301eb754c62b981f7208158a9f";
$exploded_str_array=explode('|', $str);
echo $required_str=$exploded_str_array[1];
//this contains the second part of the string delimited by |
php manual for the explode function

Related

PHP get whole string not individual params in URL

If I have a domain e.g. www.example.com/w/
I want to be able to get the whole string of text appended after the URL, I know how to get parameters in format ?key=value, that's not what I'm looking for.
but I would like to get everything after the /w/ prefix, the whole string altogether so if someone appended after the above
www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
I would be able to get https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
I was thinking of installing codeigniter on my server if that helps, but at the moment I'm just using core php
You just need to use str_replace()
$str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
$str2 = str_replace('www.example.com/w/', '', $str);
echo $str2;
Output
https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
Read more about str_replace()
Try this, with strpos and substr
$str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
echo $str.'<pre>';
$start = strpos($str, '/w/');
echo substr($str, $start + 3);die;
Output:
www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
strpos() will give you first occurrence of /w/ and from there you can do substr with +3 to remove /w/
OR Try this, with strstr and str_replace
$str = "www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html";
echo $str.'<pre>';
$str1 = strstr($str, '/w/');
echo $str1.'<pre>';
$str2 = str_replace('/w/', '', $str1);
echo $str2.'<pre>';die;
Output:
www.example.com/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
/w/https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
https://www.nytimes.com/2019/04/17/us/politics/trump-mueller-report.html
strstr() will give you substring with given /w/ and use str_replace() to remove /w/from new string

How to get last part of a string?

I have this string:
"application/controllers/backend"
I want get:
backend
of course the backend it's dynamic, so could be change, so I'm looking for a solution that allow me to get only the last part of the string. How I can do that?
You can take the advantage of basename() to get the last part
in your case, it will be
basename("application/controllers/backend");
Output:
backend
Some thing like this :
echo end(explode("/", $url));
If this thorws error then do :
$parts = explode("/", $url);
echo end($parts);
$arr = explode ("/", $string);
//$arr[2] is your third element in the string
http://php.net/manual/en/function.explode.php
Just use
basename("application/controllers/backend");
http://php.net/manual/en/function.basename.php
And, if you want to do it with a regex:
$result = (preg_match('%.*[/\\\\](.*?)$%', $url, $regs)) ? $regs[1] : '';
You did ask initially for a solution with regex, so, although the other answers haven't involved regex, here is one approach which does.
You can use preg_match and str_replace for this:
$string = '"application/controllers/backend"';
preg_match('/[^\/]+"/', $string, $matches);
$last_item = str_replace('"','',$matches[0]);
$last_item is now a string containing the word backend.

How to use explode and get first element in one line in PHP?

$beforeDot = explode(".", $string)[0];
This is what I'm attempting to do, except that it returns syntax error. If there is a workaround for a one liner, please let me know. If this is not possible, please explain.
The function array dereferencing was implemented in PHP 5.4, so if you are using an older version you'll have to do it another way.
Here's a simple way to do it:
$beforeDot = array_shift(explode('.', $string));
You can use list for this:
list($first) = explode(".", "foo.bar");
echo $first; // foo
This also works if you need the second (or third, etc.) element:
list($_, $second) = explode(".", "foo.bar");
echo $second; // bar
But that can get pretty clumsy.
Use current(), to get first position after explode:
$beforeDot = current(explode(".", $string));
Use array_shift() for this purpose :
$beforeDot = array_shift(explode(".", $string));
in php <= 5.3 you need to use
$beforeDot = explode(".", $string);
$beforeDot = $beforeDot[0];
2020 : Google brought me here for something similar.
Pairing 'explode' with 'implode' to populate a variable.
explode -> break the string into an array at the separator
implode -> get a string from that first array element into a variable
$str = "ABC.66778899";
$first = implode(explode('.', $str, -1));
Will give you 'ABC' as a string.
Adjust the limit argument in explode as per your string characteristics.
You can use the limit parameter in the explode function
explode($separator, $str, $limit)
$txt = 'the quick brown fox';
$explode = explode(' ', $txt, -substr_count($txt, ' '));
This will return an array with only one index that has the first word which is "the"
PHP Explode docs
Explanation:
If the limit parameter is negative, all components except the last
-limit are returned.
So to get only the first element despite the number of occurences of the substr you use -substr_count

How to get part of string from the end in PHP?

I am making application where I receive a string from user. The string is concatenated with - character between them. First part of string contains alphabetic data whereas later part contains integers or floating point numbers. For example: A string might be 3 Cups Tea-5.99.I want to get the later part of string 5.99 separated by - character. How to do that? I know about PHP substr() function but that takes fixed characters to retrieve substring from. But in this case the later part will not be fixed. For example: 2 Jeans-65.99. In this case I would need last 4 characters meaning that I can't use substr() function.
Anybody with solution?
I know I would need to apply regex but I am completely novice in Regex.
Waiting for your help.
Thanks!
Simply
$result = explode('-', $string)[1];
For PHP<5.4 you'll have to use temporary variable:
$data = explode('-', $string);
$result = $data[1];
Edit
As mentioned in comments, if there is more than 1 part, that will be:
$result = array_pop(explode('-', $string));
$bits = explode('-', $inputstring);
echo $bits[1];
You can use substr() with strpos():
$str = '3 Cups Tea-5.99';
echo substr($str, strpos($str, "-") + 1);
Output:
5.99
Demo!
If data will be like this: "1-Cup tea-2.99", then
$data = "1-Cup tea-2.99";
$data = explode('-', $string);
$result = $data[count($data)-1];

Get the string after a string from a string

what's the fastest way to get only the important_stuff part from a string like this:
bla-bla_delimiter_important_stuff
_delimiter_ is always there, but the rest of the string can change.
here:
$arr = explode('delimeter', $initialString);
$important = $arr[1];
$result = end(explode('_delimiter_', 'bla-bla_delimiter_important_stuff'));
I like this method:
$str="bla-bla_delimiter_important_stuff";
$del="_delimiter_";
$pos=strpos($str, $del);
cutting from end of the delimiter to end of string
$important=substr($str, $pos+strlen($del)-1, strlen($str)-1);
note:
1) for substr the string start at '0' whereas for strpos & strlen takes the size of the string (starts at '1')
2) using 1 character delimiter maybe a good idea
$importantStuff = array_pop(explode('_delimiter_', $string));
$string = "bla-bla_delimiter_important_stuff";
list($junk,$important_stufF) = explode("_delimiter_",$string);
echo $important_stuff;
> important_stuff

Categories