Hi, I am trying to figure out how to remove a couple of lines from strings. The lines that need to be removed are not always the same - the text at the start of the lines is always the same, but the end of the lines is always different. The string $html contains all the code for an html page, and I just need to remove the 2 lines starting with <meta name="description" and <meta name="keywords" For example : <html> <head> [B]<meta name="description" content="example stuff, example, stuff"/> [COLOR="Red"](should be removed)[/COLOR] <meta name="keywords" content="example stuff, example, stuff"/> [COLOR="Red"](should be removed)[/COLOR] [/B]<meta name="author" content="blah" /> <meta name="copyright" content="blah" /> </head><body>Blah</body></html> Code (markup): I need to completely remove the meta description and keyword lines, but leave the rest intact. If anybody could provide the code for this, it would be much appreciated. Thanks, Leo
$html = preg_replace ('/<meta\s+name="description.*?>/si','',$html); $html = preg_replace ('/<meta\s+name="keywords.*?>/si','',$html); PHP:
A1SiteNames.com , would you please share the complete script you are using for this ? I have to do the exact same thing and as I dont know php I am clueless.
Hi, sorry I used this to modify a commercial script that I bought, so I can't share it. But it's very simple to do this, so long as you already have the string in your script - then you can probably just place those 2 lines after the string is created. Of course you should change the $html at the start and end of each line to the name of the string you are working with.
chowbow, the test code is simple. You need a file test.htm in the same directory: <? $html = file_get_contents('test.htm'); //you can also supply url here $html = preg_replace ('/<meta\s+name="description.*?>/si','',$html); $html = preg_replace ('/<meta\s+name="keywords.*?>/si','',$html); echo $html; ?> PHP: Everything else depends on your needs.
I understand that it is simple (if you know how to do it Should the test.htm be empty ? And should I just then run test.php in the same directory and the meta tags will disappear ? And yes, now you can laugh of my naive questions hehe.
chowbow, I tested this example with contents provided by A1SiteNames.com as an example and saved as file test.htm. This code just reads contents of test.htm (you can also supply url or other filepath instead of 'test.htm'). After trimming meta description and keywords results will be outputted by echo. I.e. if you save this script as test.php and navigate your browser to http://yoursite/test.php, you'll see the contents with this tags stripped. This code does not change the input file, it's just handle it in memory and outputs it. If you need to save result, you can use an easy function file_put_contents: file_put_contents('result.htm',$html); PHP:
Thanks wmtips, is file_put_contents('result.htm',$html); only working in php5? Anyway, I have so many thins to do now and cannot straighten my head out to learn this. But I need this so I will save this thread and learn it in the near future, so your time is really appreciated.
Oh, yes, I forgot to mention, PHP 5 only. I have php 5. But on php 4 you can define substitution (see User Contributed Notes)