How to filter a string - php

I have this string:
"small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg"
I want to get only the first image link (after the word "small") and ignore all the rest (the word "boxshot" and the link after it).
How can I make it?

use it and like my ans. string is your above whole string
substr(string,0,stripos(string,"boxshot")-1);

$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
preg_match_all('~http(.*?)jpg~i',trim($string),$matches);
var_dump($matches[0][0]);

The easy way:
<?php
$str='small: http://img.exent.com/free/frg/products/666550/player_boxshot.jpg boxshot: http://img.exent.com/free/frg/products/666550/boxshot.jpg';
$parts=explode('boxshot:',$str);
$small=$parts[0];
echo($small);
?>

Try this:
$string = "small:
http://img.exent.com/free/frg/products/666550/player_boxshot.jpg
boxshot:
http://img.exent.com/free/frg/products/666550/boxshot.jpg";
// replace all 2 or more consecutive spaces with 1 space
$string = preg_replace( '/\s\s+/i', ' ', $string);
$array = explode( ' ', $string);
echo $array[1];
Hope this helps.

Related

PHP showing last string value

I want to display only last string value from string. This is my string ShopTop205/12.50R15
I want to just display this type of string
205/12.50R15
I have tried like
<?php
$catName= 'ShopTop205/12.50R15';
echo substr($catName, strrpos($catName, ' ') + 1);
?>
second way
<?php
$string = 'ShopTop205/12.50R15';
$string = explode('', $string);
$last_string = end($string);
echo $last_string;
?>
I have used substr() function also but i could not get result that i want.
how could i do this ?
You may remove the initial non-numeric chars with a regex:
$catName= 'ShopTop205/12.50R15';
$res = preg_replace('~^\D+~', '', $catName);
echo $res; // => 205/12.50R15
See the PHP demo
The pattern is ^\D+ here, and it matches any one or more (+) chars other than digits (\D) at the start of the string (^).
See the regex demo.
$catName= 'ShopTop205/12.50R15';
$result = substr($catName, 7,20);
print $result;//205/12.50R15;
Check this one
$catName= 'ShopTop205/12.50R15';
preg_match('/^\D*(?=\d)/', $catName, $m);
$pos = isset($m[0]) ? strlen($m[0]) : false;
$text = substr($catName,$pos); // this will contain 205/12.50R15
Doing it with substr() given that the length is always the same:
https://ideone.com/A4Avpt
<?php
echo substr('ShopTop205/12.50R15', -12);
?>
Output: 205/12.50R15

PHP preg_replace url get parameters in string

Thanks to #s.d.a.p.e I've come a step close but I'm not quite there yet.
What I'm trying to do is replace all instances of a string in a block of text. I want to replace something like this:
user is ?user_id=34&first_name=Ralph so is ?user_id=1 also
With this:
user is /user/34/ so is /user/1/ also
Here is the preg_replace code I'm using:
$pattern = '#\?user_id=([0-9]+)#';
$replace = '/user/$1/';
echo preg_replace($pattern,$replace,$string);
With that pattern I end up with this:
user is /user/34/&first_name=Ralph so is /user/1/ also
Thanks again.
try this:
$string = "user is ?user_id=34&first_name=Ralph so is ?user_id=1 also";
$result = preg_replace('/\?(user)_id=(\d+)(.*?)(?! )/i', '/$1/$2/$3', $string );
echo $result ;
Output:
user is /user/34/&first_name=Ralph so is /user/1/ also
DEMO
I'd use this:
$string = 'user is ?user_id=34&first_name=Ralph so is ?user_id=1 also';
$pattern = '#\?user_id=([0-9]+)\S*#';
$replace = '/user/$1/';
echo preg_replace($pattern, $replace, $string);
Where \S stands for any character that is not a space.
Output:
user is /user/34/ so is /user/1/ also
print preg_replace(
'#\?user_id=([0-9]+)\&(first_name=(?:.*))#',
'/user/$1?$2',
'?user_id=34&first_name=Ralph'
);
result :
/user/34?first_name=Ralph if get it right..

How to insert a string into another one on the last matched element?

I want to insert the string preview/ after the first forward slash from the left. So a string like:
http://www.mysite.com/343333
will become:
http://www.mysite.com/preview/343333
How do I do that with PHP?
With preg_replace:
preg_replace("/(.*)\/(\d+)$/", '$1/preview/$2', "http://www.mysite.com/343333")
This works-
$str = "http://www.mysite.com/343333";
$arr = explode("/",$str);
array_splice($arr, count($arr)-1, 0, array('preview'));
$str = implode("/",$arr);
print_r($str);
//Outputs - http://www.mysite.com/preview/343333
This would probably be the most straightforward way:
$url = sprintf('%s/preview/%s', dirname($url), basename($url));

How to trim line breaks and spaces in string using php

I have a string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ksoxFhlcfmsJGHDgakBQdAtaYOxSS96MvMLrl8OEMpKE9RtXITsXTj/JicJoSJbY+w3Mszce1vHs0YkdCZug6BRRxLM3Htbx7NGL91Op3oo9u0Jb8jdQb2jNB0MHk4j4XRiEuvf9jqO9829DB5OI+F0YhE8ZmQzVYvJYyVocMjBLRdf75AUVejP7gcKxceCT8ZcPQ9eUPjjCRynCsXHgk/GXDNMMfkG5pf9r+a+AV7EuUxD/rQwey6ePEV0FCHDgHtZK5KmPKJOYJlVl/JlDlxq+MSvmkp1o/5v4SsPAX5jO5KQ=="
I have tried the below method.
str_replace("\n", "", $key);
But it not worked.
Any suggestions..?
Thanks
TRY
$order = array("\r\n", "\n", "\r");
$replace = '';
$final = str_replace($order, $replace, $str);
var_dump($final);
NOTE : Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. so Processes \r\n's first so they aren't converted twice.
Try this,I think it is a simple way
echo str_replace(PHP_EOL, '', $key);
Try this:
echo str_replace("\r\n","", $key);
Try Below Code
str_replace(chr(13), "", $key);
May this will help you

Insert a slash in a string after every number except at the end (PHP)

i've a string
$string;
in this string i have a number, like 3743687
i wish to add a slash after every number, except at the end, so the result will be
3/7/4/3/6/8/7
is there a php function to do this, right ? any advice ? thanks
$string = '3743687';
$newString = implode('/',str_split($string));
var_dump($newString);
$string = "3743687";
$string = implode("/", str_split($string));
echo $string;

Categories