Forum Moderators: coopster

Message Too Old, No Replies

php regex problem

         

Vis3R

10:10 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



code:

<? 
$regex = "/(WebForm_PostBackOptions\(.*\,.*\,.*\,.*\,[ ]*['\"]{1})([^'\"]*)/i";
$replaced = str_replace('&quot;', '"', file_get_contents("myspace.txt"));
$replaced = preg_replace_callback($regex, "java_callback", $replaced);
function java_callback($matches)
{
return $matches[1].strtoupper($matches[2]);
}
echo $replaced;
?>

string to search in (the myspace.txt file):

BEFORE WebForm_PostBackOptions("ctl00$ctl00$Main$messagingMain$ReadMessage_Skin$AbuseButton", "", false, "", "http://collect.myspace.com/index.cfm?fuseaction=misc.contactInput&ProfileContentID=343035985&MyUserID=343035985&abuseflag=true", false, true) INBETWEEN WebForm_PostBackOptions(&quot;ctl00$ctl00$Main$messagingMain$ReadMessage_Skin$ReplyButton&quot;, &quot;&quot;, false, &quot;&quot;, &quot;http://messaging.myspace.com/index.cfm?fuseaction=mail.reply&amp;friendId=343035985&amp;type=Inbox&amp;messageID=154630601&amp;fed=True&quot;, false, false) AFTER

result:

BEFORE WebForm_PostBackOptions("ctl00$ctl00$Main$messagingMain$ReadMessage_Skin$AbuseButton", "", false, "", "http://collect.myspace.com/index.cfm?fuseaction=misc.contactInput&ProfileContentID=343035985&MyUserID=343035985&abuseflag=true", false, true) INBETWEEN WebForm_PostBackOptions("ctl00$ctl00$Main$messagingMain$ReadMessage_Skin$ReplyButton", "", false, "", "HTTP://MESSAGING.MYSPACE.COM/INDEX.CFM?FUSEACTION=MAIL.REPLY&FRIENDID=343035985&TYPE=INBOX&MESSAGEID=154630601&FED=TRUE", false, false) AFTER

Both javascript functions match the regex, but only the 2nd one gets edited. If i switch them, still only the 2nd one gets edited. What it should do, is convert the url to uppercase in the first function as it does in the seccond. Save the long string into a file named myspace.txt and try it out. I've been looking into the regex and recoded it already but same problem happens, so i'm out of ideas.

Thanks in advance!

[edited by: Vis3R at 10:11 pm (utc) on Feb. 27, 2008]

[edited by: coopster at 7:41 pm (utc) on Mar. 3, 2008]
[edit reason] added "pre" tags to format code [/edit]

coopster

11:40 pm on Feb 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I took a quick glance and I don't see you closing your pattern with an ending quotation. Try that first.
$regex = "/(WebForm_PostBackOptions\(.*\,.*\,.*\,.*\,[ ]*['\"]{1})([^'\"]*)['\"]{1}/i";

Vis3R

3:56 am on Mar 1, 2008 (gmt 0)

10+ Year Member



([^'\"]*) this part goes untill it finds an ending quotation, there it halts..

still, even if i add your regex, same thing happens, only the 2nd function gets fixed.

[edited by: Vis3R at 3:57 am (utc) on Mar. 1, 2008]

coopster

7:38 pm on Mar 1, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Your callback function is improperly structured. $matches is the entire array and always remember that the first array entry in any match is going to be the entire pattern, so $matches[0] will always contain the matched text of your full pattern. Try this (I modified your regex slightly to make it more effecient):
function java_callback($matches) 
{
return $matches[1].strtoupper($matches[2]);
}
$replaced = str_replace('&quot;', '"', $string);
$regex = "/(WebForm_PostBackOptions\([^,]+,[^,]+,[^,]+,[^,]+,\s*['\"])([^'\"]+)/i";
$replaced = preg_replace_callback($regex, "java_callback", $replaced);
print htmlentities($replaced);

Vis3R

7:24 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



That IS what my function looks like ... the forum took away the [one] part from it, don't know why. It does not let me write [ 1 ] for some reason. Anyway, it's not working. :\

[edited by: Vis3R at 7:25 pm (utc) on Mar. 3, 2008]

coopster

7:52 pm on Mar 3, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's a glitch in the forum software -- it gets confused by the square bracket indexing whenever a post is edited because the "edited" part of the message uses the same identifier in bbcode [webmasterworld.com] to indicate small text, or size 1. I fixed it in your original post now. Back to the issue ...

I did not fetch the file from an external text file as you are but merely defined a string with the text you provided above. That is the only difference that could be causing you a problem because I tested the code that I posted and it worked fine. Therefore, I am deducing that perhaps your text file that you are fetching with file_get_contents may have newlines or something in it. If so, you may need to add an "s" modifier [php.net]. If you copy/paste the code that I used here do you still have the issue? If so, does your "myspace.txt" file contain just a simple string as you show above here? Newlines? Or no newlines?

Vis3R

8:13 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



Well my code doesn't work however i try to fix it, but your does, even on the file.
Thank you very much!