Skip to content
Advertisement

Remove part of string after ## using php? [closed]

I have a file which has many lines containing ##. I want to remove the part of string after ##.

Here is what I have tried

<?php
$file = fopen("testr.txt", "r");

$NewLineExplode = explode(PHP_EOL,$file);

$NewString      = '';
foreach($NewLineExplode  as $eachLine) {
$HashExlode = explode('##',$eachLine);
$NewString .= $HashExlode[0].'<br>';
 }

  echo $NewString;

 ?>

testr.txt look like this

citycredits.ru##.widget_banner
anvidelabs.org##.widget_banners
hcpeople.ru##.widget_board_ads
newsoboz.org##.widget_center > noindex
softolet.ru##.widget_custom_html
edurusnews.ru##.widget_execphp
poznamka.ru##.widget_hja_adsense
softolet.ru##.widget_media_image
yuzhny.info##.widget_media_image > img[width="360"][height="520"]

The output should be like this

citycredits.ru
anvidelabs.org
hcpeople.ru
newsoboz.org
softolet.ru

Advertisement

Answer

    $string = fopen("testr.txt", "r");
    $NewString      = '';
    // Output one line until end-of-file
    while(!feof($string)) {
        $HashExlode = explode('##',fgets($string));
        $NewString .= $HashExlode[0].'<br>';
    }

    echo $NewString;
    fclose($string);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement