/    Sign up×
Community /Pin to ProfileBookmark

A big replace problem!

Hello all! I am wondering what to do with an issue I can’t solve from hours…
The promlem is:
I have this code:

[CODE]$html2 = <<<RR
<form method=’post’ action=’request.php’>
<input type=’hidden’ name=’broj’ value=’359878791261′>
<table bgcolor=’#80ff80′ border=’0′ cellpadding=’0′ cellspacing=’0′ width=’331′><tbody><tr bgcolor=’#ffffff’><td colspan=’4′

height=’4′></td></tr><tr><td colspan=’4′ height=’1′></td></tr>
<tr bgcolor=’#82fc74′><td class=’title’ colspan=’4′ height=’18’>&nbsp;<b>Internatinal</b> – Club Friendlies</td></tr><tr bgcolor=’#82fc74′><td class=’match-light’ width=’45’ height=’18’>&nbsp;12:41</td><td class=’match-light’

colspan=’3′ align=’right’ width=’286′>&#1071;&#1085;&#1091;&#1072;&#1088;&#1080; 16&nbsp;</td></tr><tr><td colspan=’4′ height=’1′></td></tr><tr bgcolor=’#cfcfcf’><td

width=’45’ height=’18’>&nbsp;<img src=’http://cdn.livescore.com/img/flash.gif’ border=’0′ width=’8′ height=’8′> 90′</td><td

align=’right’ width=’118′></td><td align=’right’ width=’118′>Leverkuzen</td><td align=’center’ width=’50’>? – ?</td><td

width=’118′>Frankfurt</td></tr><tr><td colspan=’4′ height=’1′></td></tr><tr bgcolor=’#cccccc’><td width=’45’

height=’18’>&nbsp;FT</td><td align=’right’ width=’118′>Hanover</td><td align=’center’ width=’50’>? – ?</td><td

width=’118′>Augsburg</td></tr><tr><td colspan=’4′ height=’1′></td></tr><tr bgcolor=’#cfcfcf’><td width=’45’

height=’18’>&nbsp;18:00</td><td align=’right’ width=’118′>Yung Boys</td><td align=’center’ width=’50’>? – ?</td><td

width=’118′>Vinterture</td></tr><tr><td colspan=’4′ height=’1′></td></tr></table>
RR;
$from = “</td><td align=’right’ width=’118′>”;
$to = “<input type=’radio’ value=” name=’utakmica’></td><td align=’right’ width=’118′>”;
$html2 = str_replace($from, $to, $html2);
echo $html2;
[/CODE]

As you can see here: [url]http://livescore-bg.net/lolo/proba2.php[/url]
everything is ok with the $from string, when before it there is “FT” or “18:00”, but in the first of the three replacements, where before “</td><td align=’right’ width=’118′>” there is ‘ , the replacement is full with errors. DO you have any idea why is that and what function am I supposed to use, to succsesfully replace the three replacement strings without errors, like the last two?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@CoutJan 17.2009 — At first the table is full with errors, you have to read a few html table tutorials. Why insert you the replace string not manual? Or with a placeholder?

Or get you the source from another page ? Then you should look at this.

It's faster and better as str_replace.

EDIT:

Ah i see you try to read the table out from http://www.livescore.com/. Okay you should look at
[code=php]
file_get_contents() ,
preg_replace() [/code]
Copy linkTweet thisAlerts:
@SodbusterJan 17.2009 — [I]preg_replace()[/I] It's faster and better as str_replace.[/QUOTE]Faster? Any tests I've seen or run have shown the opposite. Better? In some cases, yes, but not in a case like this, where both the from and to strings are absolute.
Copy linkTweet thisAlerts:
@SodbusterJan 17.2009 — I see four strings to be replaced, not three.

Here's your posted code after I attempted to apply some logic to its format, and changed it to display the raw string both before and after the replacement. (You might find that writing readable code can help a lot, especially when you ask other people to read it.)

When I run this code, it shows the replacements being made as expected.

[code=php]$html2 = <<<RR
<form method='post' action='request.php'>
<input type='hidden' name='broj' value='359878791261'>
<table bgcolor='#80ff80' border='0' cellpadding='0' cellspacing='0' width='331'>
<tbody><tr bgcolor='#ffffff'><td colspan='4' height='4'></td></tr>
<tr><td colspan='4' height='1'></td></tr>
<tr bgcolor='#82fc74'><td class='title' colspan='4' height='18'>&nbsp;<b>Internatinal</b> - Club Friendlies</td></tr>
<tr bgcolor='#82fc74'><td class='match-light' width='45' height='18'>&nbsp;12:41</td>
<td class='match-light' colspan='3' align='right' width='286'>?????? 16&nbsp;</td></tr>
<tr><td colspan='4' height='1'></td></tr>
<tr bgcolor='#cfcfcf'><td width='45' height='18'>
&nbsp;<img src='http://cdn.livescore.com/img/flash.gif' border='0' width='8' height='8'> 90'

</td><td align='right' width='118'> -- REPLACE

</td><td align='right' width='118'> -- REPLACE

Leverkuzen</td>
<td align='center' width='50'>? - ?</td>
<td width='118'>Frankfurt</td></tr>
<tr><td colspan='4' height='1'></td></tr>
<tr bgcolor='#cccccc'><td width='45' height='18'>&nbsp;FT

</td><td align='right' width='118'> -- REPLACE

Hanover</td>
<td align='center' width='50'>? - ?</td><td width='118'>
Augsburg</td></tr><tr>
<td colspan='4' height='1'></td></tr>
<tr bgcolor='#cfcfcf'><td width='45' height='18'>&nbsp;18:00

</td><td align='right' width='118'> -- REPLACE

Yung Boys</td><td align='center' width='50'>? - ?</td>
<td width='118'>Vinterture</td></tr>
<tr><td colspan='4' height='1'></td></tr>
</table>
RR;


$from = "</td><td align='right' width='118'>";
$to = "<input type='radio' value='' name='utakmica'></td><td align='right' width='118'>";
$html3 = str_replace($from, $to, $html2);
echo '<pre>';
echo 'Before replace: <br />';
echo htmlspecialchars($html2);
echo '<br />----------------------<br />';
echo 'After replace: <br />';
echo htmlspecialchars($html3);
echo '</pre>';[/code]
Copy linkTweet thisAlerts:
@pastet89authorJan 17.2009 — Guys, I don't know which is faster and better, but I know that str_replace() doesn't work in my case. Can you please tell me how to use preg_replace() in that case, because I don't understand the regex functions.. Please, help me! ?
Copy linkTweet thisAlerts:
@SodbusterJan 17.2009 — str_replace() and preg_replace() will produce the same results, provided of course they're done correctly.

Edit: Never mind. I see you've gone elsewhere for help.
×

Success!

Help @pastet89 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...