Jump to content

Recommended Posts

I was wonderin how to read a line and was wonderin if you guys could help me out here.

 

Say the line was something like this

$line = "abcdefg hijlkmnop ltwao 348333"

 

Now I want to search $line for hijlkmnop, if it is there then search for the next integer which would be "348333"

 

I want to search for the next integer not the number 348333, because 348333 will change constantly.

 

 

 

Appreciate anyones help.

Link to comment
https://forums.phpfreaks.com/topic/83070-solved-readline/
Share on other sites

It depends on how much the data changes.

 

If it always stays with:

 

<letters> <letters> <letters> <number>

 

You could simply explode() the space assuming the space stays the same, and none of the data would ever include a space.

 

You could also use regular expressions, but I don't think they would offer an advantage here, unless you want to pull just the number, regardless of how the rest of the string was formatted.

 

http://php.net/explode

 

$line = "abcdefg hijlkmnop ltwao 348333";
$e = explode(' ', $line);
echo $line[3];
//348333

Link to comment
https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422578
Share on other sites

Thank you that will be a great help I just need help with another thing, reading only one line in a file

 

lines.html

 

<html>
<head>
<title>lines</title>
<head>
<body>
a line
another line
lalala
another
</body>
<html>

 

Now I want it to read ONLY line 8 (lalala)

and load it into $keep

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422580
Share on other sites

Thanks again, I tried it but what if the file is not on the same server like i want to link it out to a url

 

http://samplesite.com/lines.html

<html>
<head>
<title>lines</title>
<head>
<body>
a line
another line
lalala
another
</body>
<html>

 

$keep = file('http://samplesite.com/lines.html');

$keep = $keep[7];

 

does not work

Link to comment
https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422589
Share on other sites

That's because file_get_contents returns the entire thing, not an array of lines....

 

You would want to do:

 

$keep = file_get_contents('http://localhost/file.html');
$keep = explode("\n", $keep);
$keep = $keep[20];
//depending on the line breaks, \n might need to be \r or \r\n.

 

When you access a string like an array, it returns that value.

 

Example:

 

$str = "Corbin";
echo $str[0]; //C
echo $str[3]; //b

Link to comment
https://forums.phpfreaks.com/topic/83070-solved-readline/#findComment-422638
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.