Jump to content

[SOLVED] PHP preg_replace help


HotshotONE

Recommended Posts

Alright, let me explain the problem.

 

I have content in the database which is called on a page. I have a editor that allows you to edit the content. The problem is that when I stick in something like this:

<input name="encrypted" value="qLQ2Uh/mvEO95+pfSOkiSZAh/9RjfCWHPEt3SlCMYNZM66/3Z+mtvR5WCIiHKPouIAy6A2E8D" type="hidden">

 

I want to replace all the "/"s with "\/"s.

 

If I use things like str_replace or just a general preg_replace then it ruins all my other tags. Even then, the str_replace will not change the text inside the ""s.

 

What I really need to do is preg_replace any /'s inside the "value" attribute. I don't really have any experience with preg_replace. Would somebody explain how I could do this or show me?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/40236-solved-php-preg_replace-help/
Share on other sites

Thanks! Almost I think...

 

$content = '<p>Some Text</p><input name="encrypted" value="KFDLJSDF/DFLJ//FDLJF/DFLJF" type="hidden" />';


$content1 = preg_match("<input name=\"encrypted\" value=\"(.*)\" type=\"hidden\">",$content,$matches);
$content1 = str_replace("/","\\/",$matches[1]);
$stringvalue1 = "<input name=\"encrypted\" value=\"(.*)\" type=\"hidden\">";
$newstringvalue1 = $content1;
$content = preg_replace($stringvalue1, $newstringvalue1, $content);

mysql_query("UPDATE `pages` SET page_content='".$content."' WHERE page_id='".$_GET['row']."'") or die (mysql_error());

 

My only problem now is getting it to keep all the text...even the "p" tags and everything else that could run through the content var like this:

<p>Some Text</p><input name="encrypted" value="KFDLJSDF\/DFLJ\/\/FDLJF\/DFLJF" type="hidden" />

 

Instead, it returns:

<p>Some Text</p><KFDLJSDF\/DFLJ\/\/FDLJF\/DFLJF />

Never mind...

I solved it with a little different code.

 

$i = preg_match_all("/value=\"([, . \/ \\ 0-9 a-z A-Z _ % + * ! ? -]*)\"/si", $content, $matches, PREG_SET_ORDER);
$i = str_replace("/","\\/",$matches[0][0]);

$a = preg_replace("/value=\"([, . \/ \\ 0-9 a-z A-Z _ % + * ! ? -]*)\"/si", "value=\"".$i."\"", $content);

echo $a;

Archived

This topic is now archived and is closed to further replies.

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