Jump to content

preg replace


doddsey_65

Recommended Posts

im having trouble using this function. im trying to change the strings within a post.

 

eg:

 

This is an example post
[attachment=file.png]

 

what im trying to do is change [attachment=file.png] to file.png.

 

Here is the code i have but it doesnt change anything.

 

$p_content = str_replace('<br>', '', $post_info[$key]['p_content']);
        $attach = preg_split('|\[attachment=|', $p_content);
        
        for($i=0; $i<count($attach); $i++)
        {
            $attach[$i] = str_replace(']', '', $attach[$i]);
            if(file_exists($config['asf_root'].'attachments/'.$attach[$i]))
            {
                $p_content = preg_replace('|\[attachment=([0-9a-zA-Z\.-_])\]|#i', $attach[$i], $p_content);
            }
        }
        echo $p_content;

 

it first gets all the attachment tags within the post then applies the preg replace to all of them. But the preg replace isnt replacing, so i must have gone wrong somewhere.

Any tips/ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/228267-preg-replace/
Share on other sites

ok it might work but the script isnt getting that far.

 

if(file_exists($config['asf_root'].'attachments/'.$attach[$i]))
            {
                $p_content = preg_replace('#\[attachment=([0-9a-zA-Z\.-_])\]#is', $attach[$i], $p_content);
            }

 

the file doesnt exist since the $attach array includes the normal text aswell as the attachment names.

i thought:

$attach = preg_split('|\[attachment=|', $p_content);

 

would have worked to just add the attachments to the array but it adds everything in the post.

Link to comment
https://forums.phpfreaks.com/topic/228267-preg-replace/#findComment-1177096
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.