defeated Posted June 26, 2008 Share Posted June 26, 2008 I'm stuck! I need to know how to phrase an expression to get rid of a second span. It goes something like this... <span style='font-weight:bold;'><span style='font-weight:bold;'>some text</span></span> Obviously I only need one span rather than two. It goes $description=preg_replace("what goes here???","",$description); My head is melting! Quote Link to comment https://forums.phpfreaks.com/topic/112017-solved-getting-rid-of-a-superfluous-ltspangt/ Share on other sites More sharing options...
effigy Posted June 26, 2008 Share Posted June 26, 2008 This will cause problems if the spans are actually different, because it will remove the additional </span>. Will this occur in your data? <pre> <?php $data = "<span style='font-weight:bold;'><span style='font-weight:bold;'>some text</span>"; echo htmlspecialchars(preg_replace('%(</?span[^>]*>)\1+%', '$1', $data)); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/112017-solved-getting-rid-of-a-superfluous-ltspangt/#findComment-575069 Share on other sites More sharing options...
defeated Posted June 26, 2008 Author Share Posted June 26, 2008 All spans should be the same. I replaced all spans with the ones above (or rather you did! thanks again) and then I replaced all <strong> tags with the same span tags.... thus the double occurance in places. I'll put in the code you just gave me and see what transpires. Quote Link to comment https://forums.phpfreaks.com/topic/112017-solved-getting-rid-of-a-superfluous-ltspangt/#findComment-575100 Share on other sites More sharing options...
sasa Posted June 26, 2008 Share Posted June 26, 2008 try <?php $text = "<span style='font-weight:bold;'>bla <span style='font-weight:bold;'>some text</span></span>"; echo preg_replace("/(?<=<span style='font-weight:bold;'>)(.*?)<span style='font-weight:bold;'>(.*?)<\/span>(?=<\/span>)/i",'\1\2',$text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/112017-solved-getting-rid-of-a-superfluous-ltspangt/#findComment-575225 Share on other sites More sharing options...
defeated Posted June 26, 2008 Author Share Posted June 26, 2008 Code starts $query=mysql_query("SELECT * FROM mytable") or die (mysql_error()); while($result=mysql_fetch_array($query)){ $initialdescription=$result['description']; then there is a bit more code with replaces etc thats all fine. Then I used $description=preg_replace('%(?span[^>]*>)\1+%','$1',$description); Then I have $description=mysql_real_escape($description); Then mysql_query("UPDATE mytable SET description='$description' WHERE description='$initialdescription'") or die(mysql_error()); } It wont work. If I remove WHERE description='$initialdescription' then it runs but it updates all the descriptions to be the same... would have been a disaster if I hadn't backed it up. If I leave it in I get a mysql error because it doesn't seem to like something caused by mysql_real_escape I presume. I'm really in the dark here.... way out of my depth. error is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text-align:center;'> Pharmacy Technician&nb' at line 5 I don't understand it. I'm told there is nothing wrong with the mysql syntax. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/112017-solved-getting-rid-of-a-superfluous-ltspangt/#findComment-575457 Share on other sites More sharing options...
effigy Posted June 27, 2008 Share Posted June 27, 2008 Run $initialdescription through mysql_real_escape_string before placing it in the SQL. Quote Link to comment https://forums.phpfreaks.com/topic/112017-solved-getting-rid-of-a-superfluous-ltspangt/#findComment-575961 Share on other sites More sharing options...
defeated Posted June 27, 2008 Author Share Posted June 27, 2008 I'd just gone for a long walk on the beach with the dog... and while I was out came up with exactly that. Nice to see it there waiting for me when I got back to confirm my thoughts! topic solved!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/112017-solved-getting-rid-of-a-superfluous-ltspangt/#findComment-576017 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.