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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.