Bluehost.com Web Hosting $6.95

Weird replacing

This is a discussion on Weird replacing within the PHP General forums, part of the PHP Programming Forums category; Hi, I wrote a code to change the urls in my forum, to a new format. I did it with ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-26-2003
Anguz Web Design
 
Posts: n/a
Default Weird replacing

Hi,

I wrote a code to change the urls in my forum, to a new format. I
did it with output buffering and inside it, I used
preg_replace_callback, which worked great for me. But I shared
this code with the forum program community and there's a person
that can't make it work. He says his Apache gets caught up in a
loop or something. So I looked for an alternative to
preg_replace_callback. The way I did it is with preg_match_all
and then changed each element in the array and saved the changed
elements in a new array, then I just str_replace using the arrays
as find/replace. This solutions worked fine, almost. I use two
functions to do changes to the urls, one to make them relative
and the other to change symbols in the query, to something more
friendly to search-engine spiders. The function to make them
relative works great with the new method, as it did with
preg_replace_callback, but the other function, the one to replace
symbols in the query, is acting in an odd way.

Here's function

<?php
function spider_url($match){
if(!strstr($match['2'], 'action') || strstr($match['2'],
'display') || strstr($match['2'], 'messageindex'))
return $match['1'] . '/' . str_replace(array("=", ";",
"&"), array("-", "_", "_"), $match['2']);
return $match['0'];
}
?>

if I call it with this

<?php
/// $scripturl is the var that has the url to the forum
script, like http://example.com/bbs/index.php
$buffer = preg_replace_callback('/(' . preg_quote($scripturl,
'/') . ')\?([\w;=~&+%]+)/i', 'spider_url', $buffer);
?>

it works great and the url

http://example.com/bbs/index.php?boa...play;thread=12
3;start=1

changes to

http://example.com/bbs/index.php/boa...play_thread-12
3_start-1

but if I do it with this

<?php
preg_match_all('/(' . preg_quote($scripturl, '/') .
')\?([\w;=~&+%]+)/i', $buffer, $match_spider, PREG_SET_ORDER);
for($i = 0; $i < count($match_spider); $i++){
$arr1_spider[$i] = $match_spider[$i]['0'];
$arr2_spider[$i] = spider_url($match_spider[$i]);
}
$buffer = str_replace($arr1_spider, $arr2_spider, $buffer);
?>

then the url comes out like this

http://example.com/bbs/index.php/boa...play;thread=12
3;start=1

It changes just the first part, up to the semicolon... I did
other tests and there was one where changed everything up to the
semicolon right before 'star'....

I can't understand what's going on, so if someone has a clue,
please tell me. Thank you very much in advance for taking your
time reading this.

Cristian
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:34 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0