doddsey_65 Posted February 20, 2011 Share Posted February 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/228267-preg-replace/ Share on other sites More sharing options...
doddsey_65 Posted February 20, 2011 Author Share Posted February 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228267-preg-replace/#findComment-1177096 Share on other sites More sharing options...
silkfire Posted February 20, 2011 Share Posted February 20, 2011 Your preg_replace should look like this: $p_content = preg_replace('#\[attachment=([0-9a-zA-Z\.-_]+)\]#s', '$1', $p_content); Quote Link to comment https://forums.phpfreaks.com/topic/228267-preg-replace/#findComment-1177121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.