Jump to content

preg_matching a "value=""" attribute


HotshotONE

Recommended Posts

Ok,

After hours of trying to solve this myself I am finally asking for help.

 

Here is the problem. I have content in a DB which I pull to a PHP file that allows you to edit it. But in some of my html tags the value="" attributes have forbidden characters like /'s that must remain in there but keep breaking the code up. I basically need to have a preg_match_all function that finds all the attribute tags and replaces the /'s with \/'s. I cannot use addslashes or anything like that because it messes up the other HTML tags.

 

Here is my code:

	$content = $data_desired['page_content'];


$i = preg_match_all("/value=\"([\/ \\ 0-9 a-z _ \+ \* ! \? \-]* )\"/six", $content, $matches, PREG_SET_ORDER);
echo "Match 1:".$matches[0][1]."<br>";
echo "Match 2:".$matches[1][1]."<br>";

$nm = count($i);
$num = 0;

while($nm > $num || $nm == $num){
$x1 = str_replace("/","\\/",$matches[$num][1]);
$x2 = str_replace("value=\"".$matches[$num][1]."\"", "value=\"".$x1."\"", $content);
$num++;
}

echo $x2."<br>"; /* Gets put in a text area where the user can edit the code instead of echo */

 

Now...I have some HTML in a DB with two value attributes. The above code works & finds & fixes both value attributes when I paste the content in the content variable like this:

$content = "blahblahblahblahblahblahblah etc...."

 

But when I pull the content from the DB it only matches the first one. I don't understand why it won't work if it is pulled from the DB. Can anybody offer me any advice?

 

Here is the exact code in the DB if you need to look at the long version: Click Here Otherwise, the short version is below.

 

This is a short example of what I have:

<p align="center">
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input name="cmd" type="hidden" value="_s-xclick" />
<strong>
<input alt="Make payments with PayPal" src="http://www.daughtersofzion.us/ordernow.gif" border="0" name="submit" type="image" />
</strong>
<input name="encrypted" type="hidden" value="-----BEGIN PKCS7-----MIIHoAYJK/oZIhvcNAQcEoIIHkTCCB40CAQE//xggEwMIIBLA/IBADCBlDCBjjELMAk/3NK0rErWobSnJWrJ4UXU8riM-----END PKCS7-----" /></p>

 

I need it to change to:

<p align="center">
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input name="cmd" type="hidden" value="_s-xclick" />
<strong>
<input alt="Make payments with PayPal" src="http://www.daughtersofzion.us/ordernow.gif" border="0" name="submit" type="image" />
</strong>
<input name="encrypted" type="hidden" value="-----BEGIN PKCS7-----MIIHoAYJK\/oZIhvcNAQcEoIIHkTCCB40CAQE\/\/xggEwMIIBLA\/IBADCBlDCBjjELMAk\/3NK0rErWobSnJWrJ4UXU8riM-----END PKCS7-----" /></p>

 

 

 

 

Link to comment
Share on other sites

<pre>
<?php
$data = <<<DATA
<p align="center">
<form method="post" action="https://www.paypal.com/cgi-bin/webscr">
<input name="cmd" type="hidden" value="_s-xclick" />
<strong>
<input alt="Make payments with PayPal" src="http://www.daughtersofzion.us/ordernow.gif" border="0" name="submit" type="image" />
</strong>
<input name="encrypted" type="hidden" value="-----BEGIN PKCS7-----MIIHoAYJK/oZIhvcNAQcEoIIHkTCCB40CAQE//xggEwMIIBLA/IBADCBlDCBjjELMAk/3NK0rErWobSnJWrJ4UXU8riM-----END PKCS7-----" /></p>
DATA;

function parse_value ($matches) {
	return preg_replace('%/%', '\/', $matches[0]);
}

$data = preg_replace_callback('/(?<=value=")([^"]+)/', 'parse_value', $data);
echo htmlspecialchars($data);
?>
</pre>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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