Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 basic questions (str_replace with slashes, integers -> strings)

Status
Not open for further replies.

748323

Programmer
Dec 10, 2004
70
US
1. Is there a function in PHP that will convert integers to their string form? 1 to one, 2 to two, etc.

2. When I try to use str_replace() to replace a slash or a bracket with anything else, I get an error. What am I doing wrong?

thanks!
 
1) Not that I know of...

2) Example?? We're not mind readers here. :)

Ken
 
Well.. I'm trying to make a program that will reverse ascii art.

$ascii = str_replace("/", "111", $ascii);
$ascii = str_replace("\", "222", $ascii);
$ascii = str_replace("111", "/", $ascii);
$ascii = str_replace("222", "\", $ascii);

I get errors when I try to run that. I also tried preg_replace with no luck, either.
 
It is the double quotes.
If the string is enclosed in double-quotes ("), PHP understands more escape sequences for special characters:

Table 11-1. Escaped characters
sequence meaning
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation
 
Still not enough information for a meaningful answer.

What does your variable $ascii contain? What errors do you get?

You need to escape the backslash with a backslash.
Code:
$ascii = str_replace("/", "111", $ascii);
$ascii = str_replace("\\", "222", $ascii);
$ascii = str_replace("111", "/", $ascii);
$ascii = str_replace("222", "\\", $ascii);

Ken
 
Well, $ascii is a user-inputted variable. The program is supposed to reverse ascii art, such as:

…………………………………………………. __ . __……………………
……………………………………………….../’`: : `\,---`.„…………………
………………………………………………../|,-‘`¯¯`\(0)_\,----,,,_………..
………………„~*¯¯`”\,………...._„_......…( `\(0),,_/`;;;;; o : : :eek: `-, …...
……………../ : ·: (“,.~;*’¯¯¯”\,(_,-- ``”~,….\ : ;/\;;;;; : : : : : : : : : :`\…..
………….....| : · :,/`,-*~;~··-„,/(‘` ``)/· ,…\.,/` ::(_): : : : :: : : : : : : : \…
…………….| : · / ,/`,--\ \’`0\,---„1 ‹’`--(_ ,// : : : :;; : : : : : : : : : : : : : |…
……………..) : µ’` .\ (0) `;;;. . ·`), . ,-~`. \: : : : : : ;;: : : : : : : : : : : :/…
……………/` ,/_~-, .;;;;„-„,__,./, ```/……. `\: : : : · · : : : : : : : : : :,-‘ ….
……………`-/¨;--;~’ `”*-=,,__`” ,) ,/`……….`› : : : :_,--*'`¯¯”~----,--`…….
…„„………__\, · ‘, · . . . `/,_,\ ,/,-,_ ……. /` ` | ./¯ · · · · · · · „-“…………
.( :·`\,-~*`¯ · · ·`¯` `~--~*~---~;/`,-~*```*--, `1` / : | · · · ,---~*”`……………
. \·:··:”*~-,,„„____„„,.-~”`¯¯¯¯/ / · · · · · ‘\,) , / : :| · · · ·\………………….
…`”’~-,„„„„„„,,~‘`` . ( · · · ,.__| | · · · ·, `\„„/ ,/`: : / · · · · ·| ………………..
…………………….\`”””` · · ·`’~;-,„,,_)”`_-‘ : : / · · · · · · | ………………..
……… (`*”-,„,-”¯¯”`-;„· · · · · · , /'``,-~”`¯: : : :/ · · · · · · |.………………..
………. | :: (,;-===-„, `\,· · · ,-`| · ·/ : : : | :,/` · · · · · · ·| ………………..
……… . \ : :\, · · · · \\ · `\. ·)· / · / : :\ : :`~,_ · · · · · ./ …………………
………… \\,_`~.,_„,.-*\\, `/,/„/` ,/ : : :`’;-.„_ : ¯-, · · · /,…………………..
…………..`\,,`”| : :`-,„_„))’`"` ·,/`_„,~*’` · · ( · , ,`)· ·,-; `’\,………………..
……………..`*-\ : : : `~----~*` / · · · · · · · · “”~”` ·/` ·( _: )………………
…………. ,.¬-,--\ : : : : : : : : / · · · · · · · · · ·,,-“`…..’-„,-‘……………….
…………..| |: :*:\ : : : : : : :_/ „____„„„„,---~*`……………………………
…………./ / : : : `~-„„„„„,.;;`,,.--`………………………………………….
…………| | : : : :|¯ : ,/ ¯ ………………….........................................
………….\,\,_,„./---~`………………………………............................
………………………………………………………………………………
Code:
$ascii = stripslashes($ascii);
$ascii = nl2br($ascii);
$line_count = substr_count($ascii, "<br />");
$pieces = explode("<br />", $ascii);


for($i=0; $i <= $line_count; $i++) {
echo strrev($pieces[$i]) . "<br>";
}

That is the code I use to actually reverse the ascii art, but some characters need to be flipped around (Such as / and \). Even though using double back slashes doesn't give me an error, it doesn't actually reverse it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top