scbookz Posted July 29, 2009 Share Posted July 29, 2009 lets say i have ------------------------------ echo $x ----------------------- and i get --------------------------- abc-123456789abcdefgh ------------------------------------ and i want to remove abc- each time ---------------------------------------- desired output = 123456789abcdefgh Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 29, 2009 Author Share Posted July 29, 2009 inother words can you use the not command with echo like not this text but the rest of the text especially if you know a word is always going to be in the text string Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 try this... echo preg_replace('/^abc-/','',$x); Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 or i guess ltrim would work too... echo ltrim($x,'abc-'); EDIT: no, it take that back... ltrim may cause adverse effects... example... $x = "abc-as98eaw8asidjikj"; echo ltrim($x,'abc-'); will output the following (remove the 'a' too), cause it will trim any of those given characters, not the string as a whole.... s98eaw8asidjikj Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 i tried both of them it did not work on either one Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 could you post more of your code? Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 what i want to do is get only part of $x the 1st 10 letters always are the same i need to remove them thanks for your help in advance <?php $file = file_get_contents("http://webpage.html"); $x = (get_doc_title($file)); echo $x; // retrieve page title function get_doc_title($file){ $h1tags = preg_match_all('/<title>(.*)<\/title>/', $file, $matches, PREG_PATTERN_ORDER); var_export($matches[1][0]);} ?> Quote Link to comment Share on other sites More sharing options...
patrickmvi Posted July 30, 2009 Share Posted July 30, 2009 In the code you posted, you're not doing anything to try and strip anything? Perhaps you undid what you had done and posted it beforehand? Can you should what you were doing to try and accomplish your goal? Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 in your function, you should "return" the title. with var_export, it is appending quotes... changed those 2 lines... $file = file_get_contents("http://www.google.com/"); $x = (get_doc_title($file)); echo preg_replace('/^Goo/','',$x); // outputs "gle" // retrieve page title function get_doc_title($file){ $h1tags = preg_match_all('/<title>(.*)<\/title>/', $file, $matches, PREG_PATTERN_ORDER); return $matches[1][0]; } Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 that code gives me an output of ------------------------- 'webpagename - Switzerland and the Alpine Region, 1994: The Most In-Depth Guide to the Beauty and Majesty of Switzerland and the Alpine Region (Fielding\'s Switzerland and the Alpine Region)' -------------------------- i want to remove the web page name Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 echo'ing $x in your code not printing anything, what you see is the results of the var_dump function... make sure you return the value from the function... Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 every time i tried return nothing worked but when i use var export i get the tag i wanted but that tag has a little too much information i tried return from advice from someone else and it never works i am getting good output to my page now but i have the whole string verses part of it maybe your saying i am printing not a string but part of an array? Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 this code ------------------------------------- $file = file_get_contents("http://page.html"); $x = (get_doc_title($file)); echo preg_replace('/web/','',$x); // retrieve page title function get_doc_title($file){ $h1tags = preg_match_all('/<title>(.*)<\/title>/', $file, $matches, PREG_PATTERN_ORDER); var_export($matches[1][0]);} ?> -----------------------------------gives me 'web - Switzerland and the Alpine Region, 1994: The Most In-Depth Guide to the Beauty and Majesty of Switzerland and the Alpine Region (Fielding\'s Switzerland and the Alpine Region)' --------------------------- i am trying to remove the word web Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 did you try the last code i posted? it works for me... it grabs Google's title and pulls off 'Goo' and returns just 'gle' Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 you have to return the value from the function to be able to parse the title which then will be stored in the var $x ... and preg_replace will remove the text you want from $x ... plus you changed the regular expression to just '/web/' ... to replace 'web' from the beginning of the string, you need the '^' character to represent the start of a string... Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 i tried it but it did nto work i am not grabbing the title of the web page but contents from a tag i tried return before this post and never worked i am searching a html source and then displaying the tag on my web site but i need to remove part of the tag return does not work with tags it seems maybe with the text yes but not with tags inside a page Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 i will give you my output error code if you have another second wheni do as you said i will copy my code and my output error Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 i know exactly what you are trying to do. i made it work with my code... do me a favor and make a new document, and paste my code into it, and you will see that it is grabbing the text between the title tags of google.com's homepage and pulling off the beginning characters... Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 good news youare a genius but you left off something return $matches[1][0]; needed to be return ($matches[1][0]); thats why i got an error the 1st time now it is working man thanks alot you get a free book if you want a book my email is ronaldmbaldwin@yahoo.com we have 10,000 books thanks again ------------------------------------------- final code if someone wants it this works and the guy above is a genius except for () missing lol ------------------------------------- <?php ini_set('display_errors', 1); ini_set('log_errors', 1); ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); error_reporting(E_ALL); $file = file_get_contents("http://website.html"); $x = (get_doc_title($file)); echo preg_replace('/^Book/','',$x); // retrieve page title function get_doc_title($file){ $h1tags = preg_match_all('/<title>(.*)<\/title>/', $file, $matches, PREG_PATTERN_ORDER); return($matches[1][0]);} ?> Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 if you ever need help with anything email me or if you need to find a good price on books here is a goodlink for you www.half.com Quote Link to comment Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 thanks, i love free things... but truthfully, you do not need the parantheses ... taken from php.net... Note: Note that since return() is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case. Quote Link to comment Share on other sites More sharing options...
scbookz Posted July 30, 2009 Author Share Posted July 30, 2009 1 more glitch seems to never end echo preg_replace('/^Book-/','',$x); Book- <------ gives and error - <------ makes error Quote Link to comment 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.