Forum Moderators: coopster

Message Too Old, No Replies

Better method for search and replace in string

str_replace but I don't know the lengths

         

twist

5:13 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is an example of the type of string I am trying to search and replace,

Generic text about *2nothing~ in particular. Just need to *5extract certain~ characters out of the text.

I need to search the string for "*", collect the number directly after the "*", then extract the text, and watch for the "~" character to denote the end of the text.

Then the information is sent to a new function i.e.

new("2", "nothing")

The new function formats the data and returns it into the original string.

Currently I am just looping through the string and replacing as I go, but I imagine their is a better and more efficient way.

interactive gun

5:51 pm on Feb 26, 2005 (gmt 0)

10+ Year Member



Are you just searching and replacing? so your final string looks like:

Generic text about 2nothing in particular. Just need to 5extract certain characters out of the text.

or are you using the data elsewhere as you go?

twist

6:10 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Final text will look like this,

Generic text about <a href="widget2/nothing.html"> in particular. Just need to <a href="widget5/extract_certain.html"> characters out of the text.

I am looking at preg_replace_callback() as a possible option but am having problems.

Tapolyai

6:46 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are the key words that you are searching for always separated by spaces? You might be able to drop the * and ~ delimeters then.

You can try a nested explode(), or have you looked at sscanf()? How about preg_split()?

All these will put parts of the string into an array element. Then scanning through a trimmed uppercased array you can replace the words with whatever string you prefer. If you leave your delimiters in, and use space for the splitting, you can use them to mark the right array element to be replaced.

twist

7:25 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I got preg_replace_callback() to work except for one problem,

I need to also pass the $size variable (in bold) to the CreateLink function but don't seem to be able to do this with the preg_replace_callback() funtion.

Code so far (note, changed * to %)


function CreateLink( $url_folder_number, $url_name, $size ) { //code }

function GetLink( $matches ) {
$output = CreateLink( $matches[ 1 ], $matches [ 2 ], $size );
return $output;
}

FormatString( $text, $size ) {
$output = preg_replace_callback( "/%(\d)(\D+)~/", "GetLink", $text );
return $output;
}

$formated_string = FormatString( $database_text_string, $size );

Any ideas on how to pass the $size variable to the GetLink function from the preg_replace_callback() function?

twist

7:52 pm on Feb 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks to all for trying to help,

I finally found the anwer, I have to use preg_replace to call a function like so,

preg_replace( "/%(\d)(\D+)~/e", "CreateLink( '\\1', '\\2', '$size' )", $text);

I found it on the php.net preg_replace_callback() comments.