WEvans Posted June 3, 2007 Share Posted June 3, 2007 In the following code I need to replace SOMENAME with a wildcard. SOMENAME changes on a regular basis so I have no constant. $pattern = "color=3FFF5E><b>"; $replace = "color=333399><b>Winner(s):<br>"; $pattern2 = "<b>Winner(s):<br>SOMENAME,"; $replace2 = "<b>Winner(s):<br>SOMENAME, "; $html = file_get_contents('http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path='.$leaguename); $html = str_replace($pattern, $replace, $html); $html = str_replace($pattern2, $replace2, $html); All it's really doing is adding a space after "SOMENAME,". If I type an actual name that's in the html file, it works fine. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/ Share on other sites More sharing options...
penguin0 Posted June 3, 2007 Share Posted June 3, 2007 % after a variable works. Quote Link to comment https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/#findComment-267278 Share on other sites More sharing options...
kael.shipman Posted June 3, 2007 Share Posted June 3, 2007 Have you looked into regular expressions? I don't know what the rest of your file string looks like, but you could try something along the lines of <?php $pattern2 = '/<b>Winner\(s\):<br>[^,]+, /'; // If the name always ends in a comma, that would be awesome. Otherwise, you'd have to come up with a more complicated expression. $replace2 = "<b>Winner(s):<br>[newName], "; $html = preg_replace($pattern2,$replace2,$html); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/#findComment-267329 Share on other sites More sharing options...
WEvans Posted June 3, 2007 Author Share Posted June 3, 2007 Thank you but that didn't work for me. It may help if I explain what I'm trying to do, a little better. First let's start with my current code. <?php $leaguename = "crazyhouse"; $pattern = "color=3FFF5E><b>"; $replace = "color=333399><b>Winner(s):<br>"; $pattern2 = "<b>Winner(s):<br>SOMENAME,SOMENAME"; $replace2 = "<b>Winner(s):<br>SOMENAME, SOMENAME"; $html = file_get_contents('http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=crazyhouse&options=1&showstats=1&shownostats=1&single=1&double=1&ss=2&mintime=0&maxtime=2359&minentry=0&maxentry=100&addtime=-75600&by_time=0'); $html = str_replace($pattern, $replace, $html); $html = str_replace($pattern2, $replace2, $html); preg_match_all('|"#0000AA">(.*?)<font size=1 color=yellow face=Arial>(.*?)</a><br><font size=1 color=white face=Arial>(.*?)</td>|is',$html,$matches,PREG_SET_ORDER); echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"175px\">"; foreach ($matches as $match){ echo "<tr><td class=\"caltable\">$match[1]<font color=#000066 class=\"callinkfnt\">$match[2]</font></a><br>$match[3]</tr></td>"; } echo "</table>"; ?> First str_replace changes font color from green to blue and adds "Winner(s):[linebreak]" right after it. Works fine no problems. This will display one of two things. Either A. Winner(s): SOMENAME Or B. Winner(s): SOMENAME,SOMENAME I want my second str_replace to change B. to Winner(s): SOMENAME, SOMENAME All it's doing is adding a space. It's purely cosmetic. The value for SOMENAME is stored in the html file I'm reading and changes it to the person's name. i.e. Winner(s): JoeSmith or Winner(s): JoeSmith,JaneDoe I've tried to change my preg_match code, but if the html file doesn't display the winners, the output is wrong. That's why I went with the str_replace. I'm very new to PHP and still learning so what may be simple to you, is beyond what I know. lol Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/#findComment-267398 Share on other sites More sharing options...
kael.shipman Posted June 3, 2007 Share Posted June 3, 2007 If all you want to do is add a space after the comma, check for the comma, then replace it with a comma-space: <?php $html = file_get_contents('http://www.....'); $html = str_replace(',',', ',$html); .... ?> Yes, it's unnecessary to replace every comma on the page with a comma-space, but it won't hurt anything since html doesn't interpret more than one space in a row. Quote Link to comment https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/#findComment-267501 Share on other sites More sharing options...
WEvans Posted June 3, 2007 Author Share Posted June 3, 2007 Thanks kael. For this particular project that will work. I would however like to know if anyone can still tell me how to use a wildcard for the same situation. I would like to know for future reference. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/#findComment-267518 Share on other sites More sharing options...
kael.shipman Posted June 4, 2007 Share Posted June 4, 2007 If you're on a POSIX-compliant system, I think you can use fnmatch() (http://us.php.net/fnmatch). I kind of forgot about that one. It uses command-line-style wildcards. Quote Link to comment https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/#findComment-267922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.