Page 1 of 1
Stripping characters from a variable and pasting them with..
Posted: Sat Apr 29, 2006 11:57 pm
by chipTM
static info....
ok, im a big time php newbie using a CMS...
I want to take a variable called $url that always returns data in this form: "../../files/web/file.name"
Ok, here is what I want to do with that. Basically I want to strip off the "../.." and put a "
http://www.myserver.com" in front of it.
I am not sure how I can achieve this, maybe there is a php function that you can tell it to strip off the first X amount of characters and then assign that to variable $newurl and then + it with the
http://www.myserver.com. I think that will work, and it even might be easier if I assign the "
http://www.myserver.com" to a variable as well. Ultimately I really would like to assign the outcome to a variable so that I would just call it as $endurl or whatever.
Thank you so much in advance for any help.
-chipTM
Posted: Sun Apr 30, 2006 12:00 am
by Burrito
not sure I fully understand what you're trying to accomplish...but I think
str_replace() is what you're after. If the needle will be variable, then you should use
preg_replace().
Posted: Sun Apr 30, 2006 12:04 am
by chipTM
Burrito wrote:not sure I fully understand what you're trying to accomplish...but I think
str_replace() is what you're after. If the needle will be variable, then you should use
preg_replace().
O wow, I have no idea what you just said, but I WILL go research it and try to figure it out on my own, as this is a time pressed matter. Thanks so much for responding so quickly. I have minimal previous programming skills, it consists of BASIC back in 1993 when i was in 11th grade. lol. I am a network admin now and work mostly with vbs and bat files to do my tedious work, but I have a php dot com website i do as a hobby and I want to make it so people can copy and paste a code for video files on my site to thier blog. Well the cms i use returns that $url value as ../../files/path/yadayada.wmv and i need to take off the first part and add my domain name so i can put the code in a textarea box that is copy and pasteable. lol i dont even think pasteable is a word.
Posted: Sun Apr 30, 2006 12:07 am
by chipTM
Burrito wrote:not sure I fully understand what you're trying to accomplish...but I think
str_replace() is what you're after. If the needle will be variable, then you should use
preg_replace().
As a added note, I just looked at those functions you suggested, and I am afraid they will replace all instances, and I dont want them to do that. I just want to gather the data from $url, strip off the front, add my server, and then assign that to a variable.
Thanks
-chipTM
Posted: Sun Apr 30, 2006 12:07 am
by Burrito
if a simple replace of static data is what you're trying to do then str_replace() is the tool:
ex:
Code: Select all
$domain = "http://www.mydomain.com";
$somevar = "../../somepath/somefile.wmv";
$newvar = str_replace("../..",$domain,$somevar);
echo $newvar;
Posted: Sun Apr 30, 2006 12:29 am
by chipTM
Burrito wrote:if a simple replace of static data is what you're trying to do then str_replace() is the tool:
ex:
Code: Select all
$domain = "http://www.mydomain.com";
$somevar = "../../somepath/somefile.wmv";
$newvar = str_replace("../..",$domain,$somevar);
echo $newvar;
Ok, so that is right on track except for this, I went to implement this, and I remembered that the $somevar is dynamic, so with every different video its "../../somepath/adifferntvideoforeverypage.wmv"
If the video title was the same everytime thsi would work. Can I use wildcards or something?
Posted: Sun Apr 30, 2006 12:34 am
by Burrito
if all you're replacing is the prefix to the path the '../..' then it won't matter...
as I said in my first post, if the needle is going to change, you'll need to use preg_replace() and write a regular expression to do your replacement.
Posted: Sun Apr 30, 2006 12:40 am
by chipTM
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="Burrito"]if all you're replacing is the prefix to the path the '../..' then it won't matter...
as I said in my first post, if the needle is going to change, you'll need to use preg_replace() and write a regular expression to do your replacement.[/quote]
You have been a great help.
I wasn't thinking at first but with your code you show me
Code: Select all
$domain = "http://www.mydomain.com";
$somevar = "../../somepath/somefile.wmv";
$newvar = str_replace("../..",$domain,$somevar);
echo $newvar;
I made this
Code: Select all
$domain = "http://www.mydomain.com";
$somevar = $url;
$newvar = str_replace("../..",$domain,$somevar);
echo $newvar;
And it worked like a charm, so now I just have to figure out how to add the <embed src> tag for myspace into the <textarea> box
Thanks so much!
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sun Apr 30, 2006 1:04 am
by chipTM
Burrito wrote:if all you're replacing is the prefix to the path the '../..' then it won't matter...
as I said in my first post, if the needle is going to change, you'll need to use preg_replace() and write a regular expression to do your replacement.
I don't know if you can help me with this as well, and this might should be posted in a different thread, but I dont know how to search for it. The CMS I use does not allow "'s for quotes to be in the code, you have to use ' or single quotes, or hyphens or whatever they are called. Do you know how i can enable them or esacape the doubles to make them legal? by the way i use e107