Delete particular word from string - php

I'm extracting twitter user's profile image through JSON. For this my code is:
$x->profile_image_url
that returns the url of the profile image. The format of the url may be "..xyz_normal.jpg" or "..xyz_normal.png" or "..xyz_normal.jpeg" or "..xyz_normal.gif" etc.
Now I want to delete the "_normal" part from every url that I receive. How can I achieve this in php? I'm tired of trying it. Please help.

Php str_replace.
str_replace('_normal', '', $var)
What this does is to replace '_normal' with '' (nothing) in the variable $var.
Or take a look at preg_replace if you need the power of regular expressions.

The str_ireplace() function does the same job but ignoring the case
like the following
<?php
echo str_ireplace("World","Peter","Hello world!");
?>
output : Hello Peter!
for more example you can see

The str_replace() function replaces some characters with some other characters in a string.
try something like this:
$x->str_replace("_normal","",$x)

$s = 'Posted On jan 3rd By Some Dude';
echo strstr($s, 'By', true);
This is to remove particular string from a string.
The result will be like this
'Posted On jan 3rd'

Multi replace
$a = array('one','two','three');
$var = "one_1 two_2 three_3";
str_replace($a, '',$var);

string erase(subscript, count)
{
string place="New York";
place erase(0,2)
}

Related

Read number after specific word

I'm trying to read a number in a link in php. But I'm not sure how to do it.
http://example.com/Productdetail.asp?SID=72&ProductID=8640
How can I read the number 8640 without reading 72 at the same time.
ProductID= <- it will always be there.
SID=X <- this one will be there sometimes and left out on other pages. and the number can change between 1-999,
Is there a way to say ProductID= "read the next 4 numbers and save in string"
Thanks
you can get this output by using explode()
$link='http://example.com/Productdetail.asp?SID=72&ProductID=8640';
$links=explode('ProductID=',$link);
echo $link[1]; //this is your number
Simple use
echo $_GET['ProductID'];//8640
You can use preg_match as
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
preg_match('/&ProductID=(\d+)?/',$str,$match);
echo $match[1];
Or you can simply use parse_str as
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
parse_str($str,$res);
echo $res['ProductID'];
You can use:
strpos to get the ProductID=
's index
substr to get the string from ProductID= until the end of the string
str_replace to replace the ProductID= part
<?php
$str = "http://example.com/Productdetail.asp?SID=72&ProductID=8640";
echo str_replace("ProductID=", "", substr($str, strpos($str, "ProductID=")));
?>

Get a text from a php variable (URL)

i have a php variable $link="http://localhost/mysite/?product=men-glasses1", and I would like to display only the test after the "=" , that is here men-glasses1.
Can someone help?.
Use parse_url to return params section then parse_str to convert it into an associative array
$link="http://localhost/mysite/?product=men-glasses1";
parse_str( parse_url( $link, PHP_URL_QUERY ), $output );
echo $output["product"];
// men-glasses1
see this demo
You can use strstr and substr:
echo substr(strstr($link, "="),1);
http://php.net/manual/de/function.strstr.php
http://php.net/manual/de/function.substr.php
You could do the following in this case:
$var = explode('=',$link);
echo $var[1]; //men-glasses1
or if it's just in stage of URL (not in a variable yet), then you can just simply access it this way:
$_GET['product'];
Or as your link may get more and more sophisticated you may have a look at regular expressions. I would recommend using this tool to generate your regexes.
Use strstr() function in php to do this.This function searches for the first occurrence of a string inside another string Your code should look like this
<?php
$link="http://localhost/mysite/?product=men-glasses1";
echo strstr($link, "=");
?>
Hope this helps you

PHP find & replace - newbie

So I have some text that I need to replace with some other text PLUS the original text itself.
It's for a Wordpress hook, here's the function I'm using:
function ad_content($content) {
if (is_single() || is_page()) {
$content = preg_replace('(A)','B',$content,1);
}
return $content;
}
add_filter ('the_content','ad_content');
The thing is that this will simply replace A with B, what I need is to replace A with A+B, so I guess I'll first have to find A and make it into a variable or array and then find it again, this time replacing it with the variable+another variable that has text B in it.
Does that sound right?
You can use the matched content in the replace parameter, it's available in the $1, $2, etc. for each group in the pattern
$content = preg_replace('(A)','$1 B',$content,1);
See the documentation of preg_replace
you can use str_replace
$str = "old";
$final = str_replace($str,"new".$str,$str);
echo $final;
Above code will output oldnew
This is called string building so you can search for that for more help, but as a start you can try
$content .= " additional content";
echo $content;
see the affect that has.
Use str_replace function :) Take a read here:
http://php.net/manual/en/function.str-replace.php

Replace a character only in one special part of a string

When I've a string:
$string = 'word1="abc.3" word2="xyz.3"';
How can I replace the point with a comma after xyz in xyz.3 and keep him after abc in abc.3?
You've provided an example but not a description of when the content should be modified and when it should be kept the same. The solution might be simply:
str_replace("xyz.", "xyz", $input);
But if you explicitly want a more explicit match, say requiring a digit after the ful stop, then:
preg_replace("/xyz\.([0-9])+/", 'xyz\${1}', $input);
(not tested)
something like (sorry i did this with javascript and didn't see the PHP tag).
var stringWithPoint = 'word1="abc.3" word2="xyz.3"';
var nopoint = stringWithPoint.replace('xyz.3', 'xyz3');
in php
$str = 'word1="abc.3" word2="xyz.3"';
echo str_replace('xyz.3', 'xyz3', $str);
You can use PHP's string functions to remove the point (.).
str_replace(".", "", $word2);
It depends what are the criteria for replace or not.
You could split string into parts (use explode or preg_split), then replace dot in some parts (eg. str_replace), next join them together (implode).
how about:
$string = 'word1="abc.3" word2="xyz.3"';
echo preg_replace('/\.([^.]+)$/', ',$1', $string);
output:
word1="abc.3" word2="xyz,3"

php getting part of a URL into a string

This question is more of a "what is the best/easiest way to do"-type-of-question. I would like to grab just the users id from a string such as
User name
I would like to parse the string and get just the "123456" part of it.
I was thinking I could explode the string but then I would get id=123456&blahblahblah and I suppose I would have to somehow dynamically remove the trash from the end. I think this may be possible with regex but I'm fairly new to PHP and so regex is a little above me.
The function parse_str() will help here
$str = "profile.php?rdc332738&id=123456&refid=22";
parse_str($str);
echo $id; //123456
echo $refid; //22
Just grab any character from id= up to & or " (the latter accounts for the case where id is put last on the query string)
$str = 'a href="/profile.php?rdc332738&id=123456&refid=22">User name</a>';
preg_match('/id=([^&"]+)/', $str, $match);
$id = $match[1];
Regex:
.*id[=]([0-9]*).*$
if you explode the string:
$string="User name"
$string_components=explode('&','$string');
The user id part (id=123456) will be:
$user_id_part=$string_components[1];
then you could do a string replace:
$user_id=str_replace('id=','$user_id_part');
And you have the user id

Categories