0

I'm trying to get the data from a website using php curl. From the output, I need to neglect all the white spaces and empty lines. I used some regex. I'm really not familiar with Regex.

Please help me solve this problem.

There is my code:

if(preg_match('/<div class="search-results-details-body">(.*?) <div class="search-results-details-footer">/s', $result, $output))
{
    $stripped_result = ltrim(strip_tags($output[1]));
    $refined_output = str_replace('  ','',$stripped_result); 
    $regex = preg_replace('[\n\n]', "\n", $refined_output);  exit();
}

and this is my output in order:

 Requirements
  Minimum 2 years of web development experience is required
  Experience with PHP, MySQL, HTML, CSS, and JavaScript are preferred
  Bachelors Degree in Computer Science or related field
  Full knowledge and experience of software development lifecycle
  Strong organisational and analytical skills
  Ability to work under pressure to meet deadlines and required quality standards
  Ability to multi-task and prioritize responsibilities
  Excellent oral and written communication skills

here i want to remove all the starting white spaces. i need to get the output like this :

  Requirements
  Minimum 2 years of web development experience is required
  Experience with PHP, MySQL, HTML, CSS, and JavaScript are preferred
  Bachelors Degree in Computer Science or related field
  Full knowledge and experience of software development lifecycle
  Strong organisational and analytical skills
  Ability to work under pressure to meet deadlines and required quality standards
  Ability to multi-task and prioritize responsibilities
  Excellent oral and written communication skills

Please suggest a good solution

Thank you

3
  • 6
    ltrim() might be better. ;) Commented Aug 18, 2014 at 12:29
  • if you want to remove all the starting whitespaces then use this regex preg_replace('^ *', "", $string); Commented Aug 18, 2014 at 12:33
  • use trim it will remove spaces and special characters from begning and end. Commented Aug 18, 2014 at 13:10

1 Answer 1

5

i want to remove all the starting white spaces, i need to neglect all the white spaces and empty lines.

Just replace with empty string

^\s+

Here is online demo

  • ^ matches start of the line/string
  • \s matches any white space character [\r\n\t\f ]

Sample code:

$re = "/^\\s+/m";
$result = preg_replace($re, '', $str);
Sign up to request clarification or add additional context in comments.

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.