What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » String manipulation in PHP

String manipulation in PHP
Author: Message:
rav0
Veteran Member
*****

Avatar
i have an avatar

Posts: 1419
Reputation: 29
35 / Male / Flag
Joined: Aug 2003
O.P. String manipulation in PHP
I'm trying to integrate an image gallery onto my website. I've chosen to temporarily use SPGM because it works without a database.

It's designed to integrate easily by just being included but it doesn't work with my website's (shoddy, homemade) friendly URL system.

Not being able to easily find what I need to modify in the SPGM code I found a different approach of buffering the output then modifying that (http://www.schilken.de/weblog/archives/2004/02/20/gallery-spgm-with-good-static-urls.htm is where my idea is from but it needs to be changed a bit).

I will have a string that is a snippet of HTML code. Inside that I need to find all links within the gallery and fix them like this:
"index.php?spgmGal=rav&spgmPic=7&title=something#spgmPicture" becomes
"/something?spgmGal=rav&spgmPic=7#spgmPicture" or even
"/something/rav/7#spgmPicture"

spgmGal and spgmPic won't always be there, and spgmPic won't be unless spgmGal is also. title will always be there (this is the heart of my friendly URLs and why the integration won't work as-is).

PS I have explained the plan I developed which I have trouble implementing because I don't know how to change the URL and I have come here to ask. If anybody knows of another way of fixing the problem of the invalid links being generated I would consider that also.
| [Image: dorsh] |

(\ /)
(O.o)
(> <)

This is Bunny. Copy Bunny into your signature to help him on his way to world domination
10-05-2007 12:50 PM
Profile E-Mail PM Web Find Quote Report
hmaster
Senior Member
****

Avatar

Posts: 716
Reputation: 24
33 / Male / Flag
Joined: Nov 2004
RE: String manipulation in PHP
This may not be the most efficient way to do it but it works.

code:
<?php
$str = "index.php?spgmGal=rav&spgmPic=7&title=something#spgmPicture";

echo transformlink($str);

function transformlink($str) {
#get title
preg_match('/title=[A-Za-z0-9 ]+/', $str, $matches, PREG_OFFSET_CAPTURE);
$title = substr($matches[0][0], 6);
   
    #check if spgmGal is there
    if (preg_match("/spgmGal=[A-Za-z0-9 ]+/", $str)) {
        preg_match('/spgmGal=[A-Za-z0-9 ]+/', $str, $matches, PREG_OFFSET_CAPTURE);
        $spgmGal = substr($matches[0][0], 8);
    } else {
        $spgmGal = null;
    }

    #check if spgmPic is there
    if(!is_null($spgmGal)) {
        preg_match('/spgmPic=[A-Za-z0-9 ]+/', $str, $matches, PREG_OFFSET_CAPTURE);
        $spgmPic = substr($matches[0][0], 8);
    } else {
        $spgmPic = null;
    }

#get spgmPicture anchor
preg_match('/#[A-Za-z0-9 ]+/', $str, $matches, PREG_OFFSET_CAPTURE);
$spgmPicture = $matches[0][0];

#return transformed link
$thelink = (!is_null($spgmGal)) ? '/'.$title.'/'.$spgmGal.'/'.$spgmPic.$spgmPicture : '/'.$title.$spgmPicture;

return $thelink;
}

?>
[Image: sig.png]
10-05-2007 10:24 PM
Profile PM Web Find Quote Report
rav0
Veteran Member
*****

Avatar
i have an avatar

Posts: 1419
Reputation: 29
35 / Male / Flag
Joined: Aug 2003
O.P. RE: String manipulation in PHP
(Y) Thanks I've got that working to fix the URL properly.

Thing is, the input that I have is a chunk of HTML code, not just the URLs by themselves. I need to search for each URL and then fix it and replace it in the chunk of code. I think I could search for anything begining with ="index.php? and ending in a double quote but I don't know *-) how to do it (as in <a href="index...">, <img src=").
| [Image: dorsh] |

(\ /)
(O.o)
(> <)

This is Bunny. Copy Bunny into your signature to help him on his way to world domination
10-06-2007 02:35 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On