Web Hosting Talk







View Full Version : PHP and replacing text at begining/end of a string


larwilliams
12-02-2008, 06:02 PM
PHP and replacing text at begining/end of a string

Say the following are your variables:
PHP Code:



$strip_at_beginning = "A Wolf ";$strip_at_end = " cheese";$string = "A Wolf must eat cheese";// in this example, after the stripping, the only thing to remain// in $string is "must eat"




How do I make it so that:
1) The contents of $strip_at_beginning are removed from the beginning of $string.
2) The contents of $strip_at_end are removed from the end of $string.
??
The variables (in my real world app) will be filled from database fields, so the length of the strings contained in them will change.
Thanks for any help





__________________LCWSoft - Canadian web hosting (based in Newfoundland)Uptime Report
lawrencewilliams (at) lcwsoft.com

larwilliams
12-02-2008, 06:45 PM
Figured it out after:
PHP Code:



$strip_at_beginning = "A Wolf ";
$strip_at_end = " cheese";
$string = "A Wolf must eat cheese";
// in this example, after the stripping, the only thing to remain
// in $string is "must eat"
// control variables
$strip_begin_len = strlen($strip_at_beginning);
$strip_end_len = '-' . strlen($strip_at_end);
// chop off end of string
$string = substr($string, 0, $strip_end_len);
// chop off beginning of string
$string = substr($string, $strip_begin_len);
// echo it
echo $string;











__________________LCWSoft - Canadian web hosting (based in Newfoundland)Uptime Report
lawrencewilliams (at) lcwsoft.com

Spacerich
12-02-2008, 06:54 PM
Here it goes...
PHP Code:



<?
$strip_at_beginning = "A Wolf ";
$strip_at_end = "cheese";
$string = "A Wolf must eat cheese";
//'A wolf' will be removed from the original string from left
$AfterLeftCut = substr($string, strlen($strip_at_beginning));ÂÂ*
//then remove 'cheese' from last
$result = substr($AfterLeftCut, 0, -(strlen($strip_at_end)));
echo $result;
?>




Result: must eat





__________________Complete Managed

larwilliams
12-02-2008, 08:12 PM
Thanks Works the same as my solution basically. Got a new problem though. Need to check if $strip_at_end is actually in $string, as in both my code and yours, it does the replace on the end regardless if it matches the full string or not.
Here is a simple implementation that echo's its output back to the browser.http://travelbugonline.ca/dev2/strip_string.php
What i'd like to do is: if $strip_at_end isn't completely matched in $string (like in the case above), simply cut the last word off $string. That would fulfill my needs





__________________LCWSoft - Canadian web hosting (based in Newfoundland)Uptime Report
lawrencewilliams (at) lcwsoft.com

azizny
12-03-2008, 12:02 AM
Is there a chance that you will have more than 1 "A wolf" and "cheese" in the same string?
If so, then I would advise you beak them up, remove first instance and put them back in.
Peace,





__________________NEW: What Is Their IP - Anyone's IP a click away.
URL Tracker, Shortener, Blocker, Unblocker, Whois and More.

blawar
12-03-2008, 12:42 AM
$begin = "A Wolf";
$end = "cheese";
$string = "A Wolf must eat cheese";
if(preg_match("/^($begin)(.*)($end)\$/i", $string, $match) || preg_match("/^($begin)(.*)\$/i", $string, $match))
{
if(!isset($match[3]))
{
echo "\"$end\" not found at the end\n";
}
echo $match[2] . "\n";
}
else
{
echo "failed\n";
}

bager
12-03-2008, 12:56 AM
preg_replace('/^(A wolf).*/','',$string,1);

blawar
12-03-2008, 01:28 AM
Quote:



Originally Posted by bager


preg_replace('/^(A wolf).*/','',$string,1);


Incorrect, thx for playing.

foobic
12-03-2008, 02:34 AM
Can I play too? Just a small change to make it a bit neater:
Quote:



Originally Posted by blawar



PHP Code:



if(preg_match("/^($begin)(.*)($end)\$/i", $string, $match) || preg_match("/^($begin)(.*)\$/i", $string, $match))









PHP Code:



if(preg_match("/^($begin)(.*)($end)?\$/Ui", $string, $match))




(the U modifier makes the match non-greedy)





__________________
Chris <ClonePanel>
"Not everything that can be counted counts, and not everything that counts can be counted" - Albert Einstein

Adam-AEC
12-03-2008, 12:26 PM
Quote:



Originally Posted by foobic


Can I play too?


I'll play too, but the specs weren't as clear as I would of liked.
PHP Code:



<?php
$string = "A Wolf must eat cheese";
$replacements = array('/^A Wolf\s/', '/\scheese/');
echo preg_replace($replacements, '', $string);
?>