Jump to content

Where is this number coming from?


music_lp90
 Share

Recommended Posts

Hi, I'm working on a script that checks for a trigger word, in this case it is {mosCountdown} and whenever it sees that, it replaces it with include($mosConfig_absolute_path . '/includes/counter.php'); The file gets included without a problem, but for some reason the trigger word {mosCountdown} gets replaced with the number "1". Can anyone tell me where this might be coming from?Here's the php that replaces the trigger:

<?phpdefined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );$_MAMBOTS->registerFunction( 'onPrepareContent', 'countdownBot' );function countdownBot( $published, &$row, &$params, $page=0  ) {global $mosConfig_absolute_path;	// Do not proceed further if the trigger is not contained in the content	if ( strpos( $row->text, 'mosCountdown' ) === false ) {		return true;	}	// expression to search for	$find = '/{mosCountdown\s*.*?}/i'; 	// replace text	$row->text = preg_replace( $find, include($mosConfig_absolute_path . '/includes/counter.php'), $row->text );	}?> 

I've tried including a couple different files and they both insert the number 1. If I just replace the trigger word with text it does not insert a 1, its only when I try to replace it with an include that it also inserts the number 1.Thanks for any help!

Link to comment
Share on other sites

Your curly braces are causing this unexplanable behavior :)Escape them inside the pattern, and everything should work fine :)This is because they have special meaning in patterns, like specifing a range. Like this, the range is invalid and it doesn't really recognises what you exactly mean. In other words, PHP reads an invalid range where you actually meant litteral signs.

Link to comment
Share on other sites

You're replacing the text with the return value of the include statement, which will return true (1) if the include succeeded. If you want to replace the text with some value that the include file calculates, then have the include file return that value. Then the include statement will return the value that was returned by the included file instead of just true or false.

Link to comment
Share on other sites

Hey justsomeguy, thanks for the help. I see what you're saying. Is there another way to do it, though? haha. I think I've done what you suggested. Here's what I did:instead of $row->text = preg_replace( $find, include($mosConfig_absolute_path . '/includes/counter.php'), $row->text ); I did $row->text = preg_replace( $find, html(), $row->text );So instead of including the file I just made a function that returns the html and a call to a javascript function. But it just prints "startTiming();Time is Running Out" to the screen instead of treating the "startTiming()" as code. I think this is probably because I'm trying to get the code to work with Joomla. I've tried playing around with output buffering a little, but I don't really understand outputbuffering all that well.Here's the html() function that I've tried instead of using include:

<?php html();function html(){ $code = "<script type=\"text/javascript\" language=\"javascript\">startTiming();</script><div id=\"show\"><div  id=\"containCount\">"		."<div id=\"containCountBody\" style=\"color: #333300;\"><p style=\"font-size:12px; text-align:center; color:#993300;\">"		."<a href=\"http://coventryloghomes.com/content/view/172/1/\" id=\"showLink\">Time is Running Out</a></p></div></div></div>";		return $code;} ?>

I also tried using include, but stuck this line into the included file return "";I also tried placing the html() function into the include file but that didn't work either.Please tell me if I'm doing anything wrong or if there's something else I can try or if I'm not being clear on anything.

Link to comment
Share on other sites

Instead of using preg_replace(), you could also split the string at the occurance of the pattern, and paste the replacing string in between :)It deals with this returning value issue of an include file, and the issue of this function not working for this situation at both the same time. (I hope :))Edit: actually, what does the questionmark mean in your pattern? If you meant it to give the special meaning of a conditional range, then it is invalid: there is already the *-sign meaning its own range. If you meant it to be litteral in the pattern, then you should escape this questionmark too.

'/\{mosCountdown(\s*)(.*)?\}/i'
(the red brackets temporarily show the interpretion, and should not be taken in the pattern)
Link to comment
Share on other sites

Instead of using preg_replace(), you could also split the string at the occurance of the pattern, and paste the replacing string in between ;)It deals with this returning value issue of an include file, and the issue of this function not working for this situation at both the same time. (I hope :P)Edit: actually, what does the questionmark mean in your pattern? If you meant it to give the special meaning of a conditional range, then it is invalid: there is already the *-sign meaning its own range. If you meant it to be litteral in the pattern, then you should escape this questionmark too.(the red brackets temporarily show the interpretion, and should not be taken in the pattern)
I actually don't know what the question mark is for. I'm borrowing that expression from another component that was already part of Joomla. The phrase that component was triggered by was {mosImage} so I figured I could use their same expression except change it to {mosCountdown}. It seems to find the triggers without any problem, it's just that I can't get it to replace exactly how I want. I either get it to replace with almost exactly what I want, except for the extra number (1) or I get it to print parts of the code literally to the screen.Here's how I've tried using split() just trying to first replace it with "Hello World" just to see if I can properly split it.$row->text = list($before, $replace, $after) = split('/{mosCountdown\s*.*?}/i', " HELLO WORLD ", 3);doing it like this I get this error Warning: split() [function.split]: REG_BADRPT in C:\wamp\www\mambots\content\mosCountdown.php on line 22or I tried$row->text = list($before, $replace, $after) = split('{mosCountdown}', " HELLO WORLD ", 3);which gives me this error Warning: preg_match() expects parameter 2 to be string, array given in C:\wamp\www\components\com_magazine\magazine.html.php on line 1308I haven't used split before, so I'm not sure what I'm doing wrong.
Link to comment
Share on other sites

It sounds like the regex is working, it's just not replacing with the correct information.

Instead of using preg_replace(), you could also split the string at the occurance of the pattern, and paste the replacing string in between
That's the same thing, replacing a piece of text with another piece of text is the same as splitting and "pasting" (however you define pasting).
I also tried using include, but stuck this line into the included file return "";
In that case you would be replacing the text with the empty string. You need to return the actual text that you want to replace with, not an empty string.The html() function should work, it returns a string for you to replace. If it isn't showing up correctly on the page (if it is displaying the Javascript code instead of running it), the first thing to do is look at what the generated HTML output shows. If this is being run through a third-party content management system then it might be stripping HTML tags or encoding them into entities or something. See what the output says.
Link to comment
Share on other sites

What I actually mean is a replacement for the preg_replace() function for this situation. Replacing a certain piece of string from a string by the return value of a function or included file, it just looks instable to me.Manually creating two strings from the input at the replacement string, removing it in the process. Then to just "add" the new string in between and glue all together. This, because preg_replace(), if I am correct, could cause unexpected things replacing with functions or return values. Doing it like this, removes all the issues related to this function :)Or am I not correct about that?I have used it many before, and experienced many difficulties getting it to work properly in odd situations. I suggest not going too complex with it.

Link to comment
Share on other sites

No, there are no issues related to using a function like this. When this line gets executed:$row->text = preg_replace( $find, html(), $row->text );First it evaluates each of the function parameters. $find and $row->text are static values, so it just substitues the values for each variable into the parameter list. Then it executes the html function and substitutes the return value of the function into the parameter list. By the time the preg_replace function gets executed all of the parameters are simple values, it doesn't send the entire html function as an argument or anything like that, unless you leave off the parens after the function name. Finally, it assigns the return value of preg_replace to the variable on the left of the equal sign.There's nothing really complex or odd about this, that is way it is done much of the time. It's no different then doing this:$temp = html();$row->text = preg_replace( $find, $temp, $row->text );The only difference is that now you have another location in memory taking up space to hold the return value.You can also pass an entire function as an argument, and execute the function inside the other function.

function func1($var){  return "func1 " . $var;}function func2($var, $func){  return "func2 " . $func($var);}echo func2('test', func1);echo func2('a long string', strlen);

Link to comment
Share on other sites

It sounds like the regex is working, it's just not replacing with the correct information.That's the same thing, replacing a piece of text with another piece of text is the same as splitting and "pasting" (however you define pasting).In that case you would be replacing the text with the empty string. You need to return the actual text that you want to replace with, not an empty string.The html() function should work, it returns a string for you to replace. If it isn't showing up correctly on the page (if it is displaying the Javascript code instead of running it), the first thing to do is look at what the generated HTML output shows. If this is being run through a third-party content management system then it might be stripping HTML tags or encoding them into entities or something. See what the output says.
Thanks for all of the help. justsomeguy, it does just display the javascript code instead of running it and yes, it is a third-party content management system (Joomla). The source code doesn't contain any of the html tags that I'm trying to insert it just inserts startTiming();Time as text printed to the screen. and they get placed into a <span> tag. So I guess I probably need to look into if it is stripping HTML tags and how to get around that.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...