Forum Moderators: coopster

Message Too Old, No Replies

rtrim with array?

         

encyclo

1:19 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK this is a very simple question, but I just can't find the answer. :)

preg_match( '/^\+\+\+ (.*)/',$file,$pagetitle);
$pagetitle = preg_replace("/<[^>]+>/","",$pagetitle);
[b]$pagetitle = rtrim($pagetitle);[/b]
echo "<title>".$pagetitle[1]."</title>\n";

So the text file contains a line starting the "+++ " that I want to use for the page title. The line may contain HTML such as

<br>
or
<b>
which I remove, then I want to remove the carriage return (and any other cruft) at the end otherwise
</title>
falls on a new line.

Am I simply misunderstanding

rtrim
?

jatar_k

1:34 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you tried explicitly stripping it using the second param?

though it should work without it as it states

encyclo

1:42 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either with or without the second parameter, it returns "r" (without quotes) instead of the expected string. Comment out the rtrim line and everything works (except the line feed is still there, obviously).

jatar_k

1:51 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just str_replace it then I guess

I assume you are using double quotes, that's the only reason I could think it wouldn't work

jatar_k

1:55 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there's this but it's not really a problem

[bugs.php.net...]

encyclo

2:33 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks,
$pagetitle = str_replace("\n","",$pagetitle);
does nothing at all. Also,
strip_tags
(which I tried before using
preg_replace
in the above example) returned the "r" result as with the
rtrim
. It's very strange as it should just work, I'm definitely missing something in the equation...

The text files were created either in Linux or Mac OSX, nothing has ever been near Windows (if that makes a difference due to the differences wiith carriage returns).

eelixduppy

2:45 am on Apr 26, 2007 (gmt 0)



$pagetitle is an array, not a string as those functions take. If you want to apply the rtrim to each element of the array you are going to have to use something like array_map [php.net]() otherwise you should just directly call the string within the array that you want to use:

preg_match( '/^\+\+\+ (.*)/',$file,$pagetitle);
$pagetitle = preg_replace("/<[^>]+>/","",$pagetitle[1]);
$pagetitle = rtrim($pagetitle[1]);
echo "<title>".$pagetitle."</title>\n";

P.S. I found internet in NC ;)

encyclo

10:25 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that does it eelix, perfect - I knew I was close. :) As I'm discarding all but the first instance item in the array (only one title for the page) I don't need the array_map. Thanks!

jatar_k

11:49 am on Apr 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice eelix, didn't notice that

eelixduppy

2:46 am on Apr 28, 2007 (gmt 0)



There is actually an error in my code above. Change this:

$pagetitle = rtrim($pagetitle);