Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/54066-wildcard-for-string-replace/
Share on other sites

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);
?>

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.