0

Hello i'm trying to find a the rest of the string knowing only the beginning of it.

Example: I have start of string Dude=99999 Number 99999 changes its not always the same but word Dude is always the same. So my question is how can i find the rest of the string after word Dude... I know in linux there is something like t*.txt but in php? help please.

SHORT: find 'dude= and read the string until '

3
  • Where are you looking for this string? In an array? A text file? Database? Commented Apr 15, 2018 at 10:38
  • php.net/manual/en/ref.pcre.php might be what you want, but also possible with simple string matching functions if we knew more about the data and what code you've already tried. Commented Apr 15, 2018 at 10:41
  • basically the string starts 'dude=999' including ' so thats the way to capture it, but there are more of string between 'goat=999' i just need to get the line with 'dude=...' Commented Apr 15, 2018 at 10:48

3 Answers 3

1

This sounds like a good match for a regex:

$regex = "/\'Dude=([0-9]+)\'/";
$str = "'Duck=123456' 'Chicken=982731' 'Dude=123487' 'Boat=129832'";

preg_match_all($regex, $str, $matches, PREG_SET_ORDER, 0);

echo $matches[0][1]; // prints: 123487

If your input string/text has line-breaks, you will probably need additional flags for the regex matcher. But there are already questions and answers for this available, so please refer to the search.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use "str_replace()" function for this

echo str_replace("dude=","","dude=99999");

Here first parameter your fix string which would be start as above , second parameter is replacement string, which is put as empty , third parameter is your actual string

Comments

0

If you're going to check all lines for different strings you can do as I have done recently:

// loop through all uploaded files:
for ($key = 0; $key < count($_FILES['file']['tmp_name']); $key++) {

  // make an array of lines from a single file:
  $txt_file = file($_FILES['file']['tmp_name'][$key]);

  // loop through this array, line by line:
  foreach ($txt_file as $line) {

    // if your file is not utf-8 fix the encoding:
    $line = iconv("windows-1251", "utf-8", $line);

    // check if this string begins with 'Dude=':
    if (substr($line, 0, strlen('Dude=')) === 'Dude=') {

      // if yes, then trim the 'Dude=' and get what's after it:
      $found_value[$key]['dude'] = trim(substr($line, strlen('Dude=')));

      // if this string begins with 'Dude=' it cannot begin with something else,
      // so we can go straight to the next cycle of the foreach loop:
      continue;

      // check if this string begins with 'Something else=':
    } elseif (substr($line, 0, strlen('SomethingElse=')) === 'SomethingElse=') {

      // if yes, then trim the 'SomethingElse=' and get what's after if:
      $found_value[$key]['something_else'] = trim(substr($line, strlen('SomethingElse=')));

      // if there's more items to find in the string use 'continue' to shorten server's work
      continue;
    }
  }
}

I've adapted and commented it for you. Also you probably want to check if file(s) was uploaded properly, so you can use file_exists and is_uploaded_file

In the end you will get an array with structure like this:

$found_value [
  1 => array()[
    'dude' => 9999999
    'something_else' => 'hello'
  ]
  2 => array()[
    'dude' => 3765234
    'something_else' => 'hello again'
  ]
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.