I have URL of file which looks like this
movieImages/1`updateCategory.PNG
it should look like this
updateCategory.PNG
you can use like this, simple
$string = 'movieImages/1`updateCategory.PNG';
$ser = 'movieImages/1`';
$trimmed = str_replace($ser, '', $string);
echo $trimmed;
output will be updateCategory.PNG
Find the position of unwanted character and then pick up the substring after that position.
$str="movieImages/1`updateCategory.PNG";
$unwanted="`";
echo substr($str,strpos($str,$unwanted)+1);
Output
updateCategory.PNG
Fiddle
That is if the string can vary in structure and size. If the first part will always remain same you can simply remove the unwanted stuff using str_replace.
echo str_replace('movieImages/1`','',$str);
Related
I am writing an HTML file using file_put_content(), but want to be able to add additional content later by pulling the current file contents and chopping off the known ending to the html.
So something along these lines:
$close = '</body></html>';
$htmlFile = file_get_contents('someUrl');
$tmp = $htmlFile - $close;
file_put_contents('someUrl', $tmp.'New Content'.$close);
But since I can't just subtract strings, how can I remove the known string from the end of the file contents?
substr can be used to cut off a know length from the end of a string. But maybe you should determine if your string really ends with your suffix. To reach this, you can also use substr:
if (strtolower(substr($string, -strlen($suffix))) == strtolower($suffix)) {
$string = substr($string, 0, -strlen($suffix));
}
If the case not play any role, you can omit strtolower.
On the other side you can use str_replace to inject your content:
$string = str_replace('</body>', $newContent . '</body>', $string);
Maybe, Manipulate HTML from php could be also helpful.
A column in my spreadsheet contains data like this:
5020203010101/FIS/CASH FUND/SBG091241212
I need to extract the last part of string after forwward slash (/) i.e; SBG091241212
I tried the following regular expression but it does not seem to work:
\/.*$
Any Idea?
Try this:
'/(?<=\/)[^\/]*$/'
The reason your current REGEXP is failing is because your .* directive matches slashes too, so it anchors to the first slash and gives you everything after it (FIS/CASH FUND/SBG091241212).
You need to specify a matching group using brackets in order to extract content.
preg_match("/\/([^\/]+)$/", "5020203010101/FIS/CASH FUND/SBG091241212", $matches);
echo $matches[1];
You could do it like this without reg ex:
<?php
echo end(explode('/', '5020203010101/FIS/CASH FUND/SBG091241212'));
?>
this will do a positive lookbehind and match upto a value which does not contain a slash
like this
[^\/]*?(?<=[^\/])$
this will only highlight the match . i.e. the last part of the url
demo here : http://regex101.com/r/pF8pS2
Make use of substr() with strrpos() as a look behind.
echo substr($str,strrpos($str,'/')+1); //"prints" SBG091241212
Demo
You can 'explode' the string:
$temp = explode('/',$input);
if (!empty($temp)){
$myString = $temp[count($temp)-1];
}
You can also use:
$string = '5020203010101/FIS/CASH FUND/SBG091241212';
echo basename($string);
http://www.php.net/manual/en/function.basename.php
$text = "
<tag>
<html>
HTML
</html>
</tag>
";
I want to replace all the text present inside the tags with htmlspecialchars(). I tried this:
$regex = '/<tag>(.*?)<\/tag>/s';
$code = preg_replace($regex,htmlspecialchars($regex),$text);
But it doesn't work.
I am getting the output as htmlspecialchars of the regex pattern. I want to replace it with htmlspecialchars of the data matching with the regex pattern.
what should i do?
You're replacing the match with the pattern itself, you're not using the back-references and the e-flag, but in this case, preg_replace_callback would be the way to go:
$code = preg_replace_callback($regex,'htmlspecialchars',$text);
This will pass the mathces groups to htmlspecialchars, and use its return value as replacement. The groups might be an array, in which case, you can try either:
function replaceCallback($matches)
{
if (is_array($matches))
{
$matches = implode ('', array_slice($matches, 1));//first element is full string
}
return htmlspecialchars($matches);
}
Or, if your PHP version permits it:
preg_replace_callback($expr, function($matches)
{
$return = '';
for ($i=1, $j = count($matches); $i<$j;$i++)
{//loop like this, skips first index, and allows for any number of groups
$return .= htmlspecialchars($matches[$i]);
}
return $return;
}, $text);
Try any of the above, until you find simething that works... incidentally, if all you want to remove is <tag> and </tag>, why not go for the much faster:
echo htmlspecialchars(str_replace(array('<tag>','</tag>'), '', $text));
That's just keeping it simple, and it'll almost certainly be faster, too.
See the quickest, easiest way in action here
If you want to isolate the actual contents as defined by your pattern, you could use preg_match($regex,$text,$hits);. This will give you an array of hits those bits that were between the paratheses in the pattern, starting at $hits[1], $hits[0] contains the whole matched string). You can then start manipulating these found matches, possibly using htmlspecialchars ... and combine them again into $code.
I have ran into a problem.
I want to replace only the part in parenthesis, but the problem is if the value in parenthesis is 1 or something else in the same part ([x(xxx:x:x)xx];), it will end up replacing that also, which I need the x(value) at the end of each part.
[0(267:0:0)x1];[1(257:0:0)x1];[2(256:0:0)x1];[3(258:0:0)x1];[4(261:0:0)x1]; [5(262:0:0)x64];[6(320:0:0)x10];[7(17:0:0)x32];[8(295:0:0)x2];[9(35:0:0)x2];[10(44:1:1)x6];[11(0:0:0)x1];[12(0:0:0)x1];[13(0:0:0)x1];[14(0:0:0)x1];[15(0:0:0)x1];[16(0:0:0)x1];[17(0:0:0)x1];[18(0:0:0)x1];[19(0:0:0)x1];[20(0:0:0)x1];[21(0:0:0)x1];[22(0:0:0)x1];[23(0:0:0)x1];[24(0:0:0)x1];[25(0:0:0)x1];[26(0:0:0)x1];[27(0:0:0)x1];[28(0:0:0)x1];[29(0:0:0)x1];[30(0:0:0)x1];[31(0:0:0)x1];[32(0:0:0)x1];[33(0:0:0)x1];[34(0:0:0)x1];[35(0:0:0)x1];[103(0:0:0)x1];[102(0:0:0)x1];[101(0:0:0)x1];[100(0:0:0)x1];
I cannot get this working. If I try to replace a 1 value in the part in parenthesis, it ends up changing all of the 1's on the printed part.
Also keep in mind that the zero's in the parenthesis won't always be 0's.
Here's what I tried:
$items = $row['inventory'];
$test = str_replace('267', '<img href="../assets/images/items/iron_sword.png">', $items);
echo $items;
I only tried one line of code to see how it would work.
I'm trying to replace the part in parenthesis with an image (for example, the first one is [0(267:0:0)x1];. I want to replace (267:0:0) with an image...
If you want to replace all (*:*:*) pattern with the same image, you cant try preg_replace:
$str = "[0(267:0:0)x1];[1(257:0:0)x1];[2(256:0:0)x1];[3(258:0:0)x1];[4(261:0:0)x1];";
$res = preg_replace("/\(\d+\:\d+\:\d+\)/",'<img href="../assets/images/items/iron_sword.png">', $str);
You can see the result here: http://codepad.org/UIjGKv6n
If you want to replace different item with different image, then you need an translate map like this:
$from = array(
'(267:0:0)',
'(257:0:0)',
...
);
$to = array(
'<img href="../assets/images/items/iron_sword.png">',
'<img href="../assets/images/items/other.png">',
...
);
$res = str_replace($from, $to, $str);
And if you have other requirements, you can just modify the code as you need.
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"