phpBB2Refugees.com Logo
Not affiliated with or endorsed by the phpBB Group

Register •  Login 

Continue the legacy...

Welcome to all phpBB2 Refugees!Wave Smilie

This site is intended to continue support for the legacy 2.x line of the phpBB2 bulletin board package. If you are a fan of phpBB2, please, by all means register, post, and help us out by offering your suggestions. We are primarily a community and support network. Our secondary goal is to provide a phpBB2 MOD Author and Styles area.

String replace ?


 
Search this topic... | Search phpBB2 Discussion... | Search Box
Register or Login to Post    Index » phpBB2 Discussion  Previous TopicPrint TopicNext Topic
Author Message
JLA
Board Member



Joined: 30 Apr 2009

Posts: 451
Location: U.S.A


flag
PostPosted: Tue Feb 07, 2012 4:48 pm 
Post subject: String replace ?

Quick question. having a bit of a brain fog

In a string that contains the following for example

$string = test test test text test text http://testlink.com/test.php?blahblahblah test test text test text test text

What would be the best method for finding everything after testlink.com/test.php? until the 1st whitespace and replacing it with something else as follows

$string = test test test text test text http://testlink.com/test.php?thisstuffwasreplaced test test text test text test text

Thing to keep in mind that replacement must be specifically up to the 1st whitespace after testlink.com/test.php?

Thanks in advance

_________________
http://www.jlaforums.com
Back to top
drathbun
Board Member



Joined: 24 Jul 2008

Posts: 729
Location: Texas


flag
PostPosted: Wed Feb 08, 2012 1:42 pm 
Post subject: Re: String replace ?

Can the ? become your search index, or will there potentially be additional question marks in the text? Are you looking for a php solution or a database update SQL statement?
_________________
phpBBDoctor Blog
Back to top
JLA
Board Member



Joined: 30 Apr 2009

Posts: 451
Location: U.S.A


flag
PostPosted: Wed Feb 08, 2012 2:47 pm 
Post subject: Re: String replace ?

I'm looking for a php string replace statement

The search will need to be finding the specific thing "testlink.com/test.php?" in the string and replacing everything after only that specific thing until it reaches the 1st white space with something else that will be specified. I used in the example "thisstuffwasreplaced" as the replacement text.

So example
$string = test test test text test text http://testlink.com/test.php?blahblahblah test test text test text test text

will become

$string = test test test text test text http://testlink.com/test.php?thisstuffwasreplaced test test text test text test text

_________________
http://www.jlaforums.com
Back to top
drathbun
Board Member



Joined: 24 Jul 2008

Posts: 729
Location: Texas


flag
PostPosted: Wed Feb 08, 2012 3:51 pm 
Post subject: Re: String replace ?

There would probably be a regular expression way to do this, but as a brute force solution you could use a string search to find where the
Code:
testlink.com/test.php?
part appears, and from there find the next space, and then strip out what's in between the end of test.php? and the space using substring and concatenation operations.
_________________
phpBBDoctor Blog
Back to top
JLA
Board Member



Joined: 30 Apr 2009

Posts: 451
Location: U.S.A


flag
PostPosted: Mon Feb 20, 2012 5:58 am 
Post subject: Re: String replace ?

Ok, so came up with this. It works as intended with one big problem.

The php processes go way overboard with CPU and memory usage. Out of control.

Any ideas?? Has something been missed??

Example code below

Code:
<?php


    function replaceUrl( $search, $replace, $input, $url = 'http://testlink.com/test.php?' ) {
        $lastStart = 0;
        $output = '';
        while( ($lastStart<strlen($input)) && ($start = strpos($input,$url,$lastStart)) ) {
            $output .= substr($input,$lastStart, $start-$lastStart);
            $query = strlen($url);
            $stop = strpos($input,' ', $start);
            $queryPart = substr($input,$start+$query,$stop-$query-$start);
            if ( strpos($queryPart,$search) === false )
               $queryPart = '';
            else
               $queryPart = $replace;
             $output .=
                   substr($input,$start,$query) .
                   $queryPart ;
                   //$replace;
            $lastStart = $stop;
        }
        $output .= substr($input, $stop);
        return $output;
    }


    $string = '1test test test text test text! http://testlink.com/test.php?donotReplaceblahblahblahaandLeave_this !2test test text test text test text. text! http://testlink.com/test.php?donotReplaceblahblahblahaandve_this !test <br>3) http://testlink.com/test.php?donot 4) http://google.com?blahblahblaha END.';

    echo "<b>source:</b><br>".$string ;
    echo "<br><b>result:</b><br>".replaceUrl('blahblahblaha', '<b>NewStuff</b>', $string);


?>

_________________
http://www.jlaforums.com
Back to top
JLA
Board Member



Joined: 30 Apr 2009

Posts: 451
Location: U.S.A


flag
PostPosted: Mon Mar 05, 2012 8:23 pm 
Post subject: Re: String replace ?

Ended up abandoning that solution path as it appears that the function as written would consume entirely too many resources under load.

Ended up doing a simpler string replace in a couple places that achieved a better result than 1st planned.

Thanks everyone for their input.

_________________
http://www.jlaforums.com
Back to top
Display posts from previous:   
Register or Login to Post    Index » phpBB2 Discussion  Previous TopicPrint TopicNext Topic
Page 1 of 1 All times are GMT - 4 Hours
 
Jump to:  

Index • About • FAQ • Rules • Privacy • Search •  Register •  Login 
Not affiliated with or endorsed by the phpBB Group
Powered by phpBB2 © phpBB Group
Generated in 0.0474 seconds using 16 queries. (SQL 0.0081 Parse 0.0007 Other 0.0385)
phpBB Customizations by the phpBBDoctor.com
Template Design by DeLFlo and MomentsOfLight.com Moments of Light Logo