Forum Moderators: coopster
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.
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.
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?