Jump to content

[SOLVED] how do i remove a portion of a repeat string from echo


scbookz

Recommended Posts

lets say i have 

------------------------------

echo $x

-----------------------

and i get

---------------------------

abc-123456789abcdefgh

------------------------------------

and i want to remove  abc- each time

----------------------------------------

desired output  =  123456789abcdefgh

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

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]);}

?>

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?

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];
}

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

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?

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

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...

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

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...

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

[email protected]

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]);}

 

?>

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. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.