Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URL string manipulation for Slide Show

Status
Not open for further replies.

das1406

Instructor
Jul 14, 2006
10
NZ
Hi
Prelude:
This is a continuation of a thread at:
and also a previous post, regards to which I am thankful to Dan (RayPreachersSon??)for answering a question there. Since I have more questions, with enough meat to fill a franfurter factory, I decided on a new post.
Background:
In the slide show code there are, say, three images put in an array, (with their URLs or directory filenames):

if (document.images) {
adImages = new Array(
"/Dir/Sub/Subsub/Hawaiian_Hula.jpg",
"/Dir/Sub/Subsub/Halloween_Party.png",
"/Dir/Sub/Subsub/Dog's_bones,_&_cats.gif");
. . .
Further in the program it refers to a text field where a description of the slide appears beside it. The original "description" was just the URL: in the above mentioned thread Dan showed code for shortening these to:
Hawaiian_Hula
Halloween_Party and
Dog's_bones,_&_cats
. . .which is far more effective.
QUESTIONS:
1. Is it possible to edit the above further to replace, for example, the underscore with a space?
2. For other characters eg the apostrophe ', comma , and ampersand & the filename is probably (almost certainly??!;o) made invalid. eg
"/Dir/Sub/Subsub/Dog's_bones,_&_cats.gif");
To bypass this, it could be renamed as:
"/Dir/Sub/Subsub/DogQs_bonesX_Z_cats.gif");
and a Javascript function used for the textfield to replace the character Q with an apostrophe. (I realize there is a restriction on there being no Q, X or Z in the URL/filenames - that's not a big issue.
Actually, 1. and 2. could be combined into a Javascript function to replace a given character with any other (eg like a cypher code.) What would this look like?
3. In the above URLs the /Dir/Sub/Subsub/ is common to all three. Is it possible to truncate up to and including the last slash character / in the case of files in different directories?
4. I have read in the PHP language of a way to verify, or alter strings using preg_match (?) Is it possible to embed PHP code within Javascript - Head & Body sections? - of HTML?
As this is a Javascript forum, I'm not expecting m/any answers to 4. I'd really like answers to 1. & 2. - 3. would be a bonus. Again, any help/comments would be most appreciated.
David
 
I'll answer these out-of order, as Q3 doesn't require me to look anything up ;-)

3: You can use the "lastIndexOf" method of the string object to find the last instance of a character in a string... so something like this:

Code:
var oldStr = '/Dir/Sub/Subsub/Hawaiian_Hula.jpg';
var newStr = oldStr.substr(oldStr.lastIndexOf('/') + 1);

would strip out anything before the last "/" character.

1, 2, 4: If you are using PHP, then use the PHP server-side. You can't embed it client-side. However, JS does have pretty powerful RegExp functionality (which I'm really bad at), so it might be worth reading up on this (using the match, text, and replace methods of the string object).

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top