Warning: fopen(graphic_design/files/thread-1826-1.txt) [function.fopen]: failed to open stream: Permission denied in /graphic_design/global.php on line 421
file NOT opened preg_replace backreferencing in OOP -
PDA

View Full Version : preg_replace backreferencing in OOP


Koobi
09-08-2004, 03:07 AM
I posted this at phpfreaks.com but I haven't got a reply yet and I'm running out of time so I thought I'd try my luck here heh heh:




I'm using preg_replace to extract URL's and replace them with the URL encoded version of them. All this is within a class and the problem is, it's a member function of that class that does the urlencoding and then I place the member functions name as the replace parameter in preg_replace, I use backreferncing and this backreference value gets sent as a literal value and the url encoding function encodes the number value of the backreference instead of the backrefernce itself.
I thought of using preg_replace_callback as well but what would the callback value be?

I hope you can help me :)


The function that extracts the URL's and attempts to encode them:

function showPage($content, $title)
{
$validContent = NULL;
$title = $this->validData($GLOBALS['PAGE_TITLE'] . ' - ' . $title);
$validContent = preg_replace('/<\s*a\s+(?:[^href]+)*\s*href\s*=\s*["|\']+(?:http:\/\/|w{3}\.)?(.*)["|\']+/im', $this->validData("\\2", TRUE), $content);
}



The function that encodes the URL:

function validData($data, $url = FALSE)
{
$data = trim($data);
$data = htmlspecialchars($data, ENT_QUOTES);
if($url === TRUE)
{
$data = rawurlencode($data);
}

return $data;
}




Thanks for your time :)

Octane
09-09-2004, 07:50 AM
from what i can see, the function showPage is kinda useless since you are returning $data to it, but doing nothing with it.
- none of the variables inside are member variables ($this->something),
- it doesn't return anything (return $val),
- it doesn't have any passing perimeters returned (function functionname($val1, &$returnval)),
- nothing is global or static (global $var5; static $val6; )
- and it doesn't print anything out (echo ""; print "";)

... maybe i'm missing something?

Koobi
09-09-2004, 12:07 PM
Ah, heh heh I didn't post the entire contents of the Class file because it was long.
Here it is: http://koobi-studio.com/gf/octane.txt

It isn't complete yet but the two functions I mentioned are present in it. Even if what you mentioned are fixed in the script, that would still nto solve my problem with backreferencing. Any ideas?



PS:
Here's index.php if you need it for some reason:

<?php
define('INCLUDE_SET', 'YES');
if(file_exists('lib/koobi/koobi.class.php'))
{
@require_once 'lib/koobi/koobi.class.php';
}
$obj = new Koobi;
@require_once $obj->ssiCheck('lib/koobi/cornerstone.class.php');
@require_once $obj->ssiCheck('config.php');
$obj->authenticate();

$action = '?' . htmlspecialchars($_SERVER['QUERY_STRING']);

$_GET['page'] = $obj->argCleaner($_GET['page']);
if(!empty($_GET['page']))
{
require_once $obj->ssiCheck($GLOBALS['PATH_TO_INC'] . $_GET['page'] . '.inc.php');
}
else
{
require_once $obj->ssiCheck($GLOBALS['PATH_TO_INC'] . $GLOBALS['DEFAULT_FILE']);
}
echo $obj->showPage($content, $title);
$obj->destr();
?>



All the include files have a variable named $content within heredocs that is passed as a method argument via echo $obj->showPage($content, $title); in index.php and that is how I pass my content data into showPage

Octane
09-15-2004, 09:43 AM
maybe i don't understand what you mean by backreferencing?

Koobi
09-15-2004, 02:15 PM
Hey, thanks for your time Octane :)

What I mean by backreferncing is for example:

%(<([\w]+)[^>]*>)(.*)(</\\2>)%


That regular expression would match any XHTML tag because:

( //Start capturing the first pattern
< //Look for a greater than sign
( //Start capturing a subpattern in the first main pattern (second one)
[\w]+ //Look for one or more words
) //Stop capturing the subpattern in the first main pattern (second one)
[^>]* //Look for zero or more chars that don't have a less than sign in them
> //Look for a less than sign
) //Stop capturing the first pattern
(.*) //Look for zero or more characters and capture this third pattern
( //Start capturing third pattern
</ //Look for a greater than sign followed by a slash
\\2 //Look for the characters that were captured in the second pattern
> //Look for a less than sign
) //Stop capturing


I've bolded the backreferencing bit :)
Thanks again.

Koobi
09-15-2004, 02:26 PM
:EDIT:
As you can see, I have used \\2 as my backreference value but it won't work since only one is actually captured but I used that value while was playing around testing it but even using my backreference as \\1 didn't work. The value was passed literally instead of as a backreference.
If the backreference value is passed properly within my class, then this line should pass the URL steeldolphin-forums.com into my method named validData:

//Assume $content contains the <a href="http://steeldolphin-forums.com">SDC</a>
$validContent = preg_replace('/<\s*a\s+(?:[^href]+)*\s*href\s*=\s*["|\']+(?:http:\/\/|w{3}\.)?(.*)["|\']+/im', $this->validData("\\2", TRUE), $content);

Octane
09-18-2004, 12:07 PM
Well, I was about to say to give up because I was running into the same problems ... even with slightly modified examples from php.org ... then i figured it out:

1 - you need to have the /e option
2 - you need to pass your function as a string

function test ($str)
{
echo $str;
}
$content = '<a href="http://steeldolphin-forums.com">SDC</a>';
$validContent = preg_replace('/<\s*a\s+(?:[^href]+)*\s*href\s*=\s*["|\']+(?:http:\/\/|w{3}\.)?(.*)["|\']+/ime', "test('\\1', TRUE)", $content);