?
Solved

Searching for string inside preg_replace/preg_match findings

Posted on 2008-10-22
7
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

801 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