Solved

Searching for string inside preg_replace/preg_match findings

Posted on 2008-10-22
7
292 Views
Last Modified: 2013-12-13
I have on my website a forum that allows user input with bbcode style tags.
One of my issues recently has been users using CSRF on other pages on my website that aren't protected. And although I've started adding protection to each page, I feel it might be a faster immediate fix to prevent users including the string ".php" in any [img] tags, since the entire section of my website is coded in php.

I currently use the code attached below, but I am at a loss for how to ensure that ".php" does not occur within the tags. My other option is to ensure the extension of the link is a proper image extension. But again, no clue how to proceed.

Any help would be helpful :)
Thanks.
$s = preg_replace("/\[img\]([^\s'\"<>]+?)\[\/img\]/i", "<img style=\"border: none;\" src=\"\\1\" alt=\"img\" />", $s);

Open in new window

0
Comment
Question by:HigherIQ
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22784423
Does it have to be done in one line?  
Couldn't you just add another line to preg_replace .php with nothing?

$s = preg_replace("/\.php/ig", "");

Open in new window

0
 
LVL 16

Expert Comment

by:sh0e
ID: 22784430
typo
$s = preg_replace("/\.php/ig", "", $s);

Open in new window

0
 

Author Comment

by:HigherIQ
ID: 22784602
Unfortunately not, as the forum itself often references pages throughout the website with links etc.
I cannot just remove the ".php" from the whole text, as someone with an image may also have a valid link, and reason to have ".php" in the rest of the forum post.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 22785326

$s = preg_replace("/\[img\]((?:(?!\.php)[^\s'\"<>])+?)\[\/img\]/i", "<img style=\"border: none;\" src=\"\\1\" alt=\"img\" />", $s);

Open in new window

0
 

Author Comment

by:HigherIQ
ID: 22788378
ddrudik, that is absolutely perfect. I can now view it to see how it's used as well.

Thanks a lot :)
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22788553
Glad I could help.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22789349
Thanks for the question and the points.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question