Solved

Searching for string inside preg_replace/preg_match findings

Posted on 2008-10-22
7
325 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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