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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

?>

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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