Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How Can I Cut Everything After A Specific Character?

cut

  • Please log in to reply
5 replies to this topic

#1 alex1

alex1

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts

Posted 05 May 2012 - 07:19 AM

Hello guys!!!
I'm a super duper php newbie...I mostly code in java,but here I need to know somthing about php.

I have a text file with 1 word per line. - It looks like: lemon_juicy
Is it possible to cut everything after "_"

Please,someone,give me an example on how to read the file line by line and how to cut this thing...
I have tried it with 2 different ways and nothing came out... :sad:

Any replay is highly appreciated!
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 05 May 2012 - 07:26 AM

What did you try? Perhaps you were 99% there and just missed one simple thing.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 alex1

alex1

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts

Posted 05 May 2012 - 07:32 AM

The last thing I tried was:


$mytxt="test.txt";
$new_var = substr($mytxt, 0, strpos($mytxt, "_");


and also:

$string = substr($string, 0, strrpos($string, '-'));

Nothing came out...I'm sure the problem is in the sntax,because as I said I have no idea of php.
  • 0

#4 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 05 May 2012 - 08:48 AM

Perhaps the explode () and implode () functions?

$a= "Hello, World!"; 
$b= explode (", ", $a); 
print $b[0]; 
print $b[1];

And implode ():
$arr= array ("Hello", "World!"); 
$text= implode (" ", $arr); 
print $text;


Also, here are links to substr () and strpos () reference:
http://www.php.net/m...tion.substr.php
http://www.php.net/m...tion.strpos.php


* * *


Also, about this:

$mytxt="test.txt";
$new_var = substr($mytxt, 0, strpos($mytxt, "_");


How is that supposed to work? Isn't that code supposed to set $new_var to $mytxt ?

If you want to cut out everything after the '.' , for example, you might do something like this:
$mytxt="test.txt";
$new_var = substr($mytxt, 0, strpos($mytxt, ".");

I didn't test the above code, but I think it should work.


Also, if the point is to cut out the file extension, why not try exploding the string with "." and then disregarding the last element, unless there is only one? Like this (more pseudo-code than PHP):
a= explode ".", filename 
length (a) = length (a) - 1 
no_extension_filename= implode ".", a

  • 1
Regards,
RR

#5 alex1

alex1

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts

Posted 05 May 2012 - 09:29 AM

Thanks RhetoricalRuvim!!!
I will try this!


How is that supposed to work? Isn't that code supposed to set $new_var to $mytxt ?


I have no idea what is the meaning of these commands. :)

As I said I'm mainly coding in java,but as I can see php is not hard to learn if you have the rite people to help you! :)

THANKS gain!!!!

p.s:


$mytxt="test.txt";
$new_var = substr($mytxt, 0, strpos($mytxt, ".");

I'm getting a strange error:
Parse error: syntax error, unexpected ';' in /home/cbeast/public_html/investrq.com/test/test.php on line 3

  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 06 May 2012 - 04:48 AM

You missed the closing parenthesis for substr.
  • 1

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download