Читайте в текстовом файле линию за линией php - новая строка, не обнаруживаемая

Это то же самое, потому что вы используете HTML первого элемента с этим классом, вы должны циклически обработать их и заменить следующим образом:

$('span.clsCatOffCount').each((i,e) => {
  var newHtml = $(e).html().replace(/\((\w+)\)/g, '$1');
  $(e).html(newHtml)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="SbCatMenu" class="__web-inspector-hide-shortcut__">
  <dl id="dlCatLvl1" class="clsCatLvl1 clsOffCat1">
    <dd class="clsCatTree1 clsCTree1" id="CatImg1"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=1">Backdrop<span class="clsCatOffCount"> (2)</span></a></dd>
    <dd class="clsCatTree1 clsCTree1" id="CatImg2"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=2">Banner Stands<span class="clsCatOffCount"> (12)</span></a></dd>
    <dd class="clsCatTree1 clsCTree1" id="CatImg3"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=3">Data Sheet<span class="clsCatOffCount"> (16)</span></a></dd>
    <dd class="clsCatTree1 clsCTree1" id="CatImg4"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=4">Giveaways<span class="clsCatOffCount"> (9)</span></a></dd>
    <dd class="clsCatTree1 clsCTree1" id="CatImg5"><a href="../OeCart/OeFrame.asp?PmSess1=1182517&amp;SXREF=5">Table Top<span class="clsCatOffCount"> (0)</span></a></dd>
  </dl>

</div>
[116 ]

33
задан Petah 14 July 2011 в 10:57
поделиться

4 ответа

32
ответ дан 27 November 2019 в 18:21
поделиться

What's wrong with file()?

foreach (file($fileName) as $name) {
    echo('<tr><td align="center">'.$name.'</td></tr>');
}
11
ответ дан 27 November 2019 в 18:21
поделиться

From the man page of fgets:

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

Also, have you tried the file function? It returns an array; each element in the array corresponds to a line in the file.

Edit: if you don't have access to the php.ini, what web server are you using? In Apache, you can change PHP settings using a .htaccess file. There is also the ini_set function which allows changing settings at runtime.

6
ответ дан 27 November 2019 в 18:21
поделиться

This is a classic case of the newline problem.

ASCII defines several different "newline" characters. The two specific ones we care about are ASCII 10 (line feed, LF) and 13 (carriage return, CR).

All Unix-based systems, including OS X, Linux, etc. will use LF as a newline. Mac OS Classic used CR just to be different, and Windows uses CR LF (that's right, two characters for a newline - see why no one likes Windows? Just kidding) as a newline.

Hence, text files from someone on a Mac (assuming it's a modern OS) would all have LF as their line ending. If you're trying to read them on Windows, and Windows expects CR LF, it won't find it. Now, it has already been mentioned that PHP has the ability to sort this mess out for you, but if you prefer, here's a memory-hogging solution:

$file = file_get_contents("filename");
$array = split("/\012\015?/", $file); # won't work for Mac Classic

Of course, you can do the same thing with file() (as has already been mentioned).

5
ответ дан 27 November 2019 в 18:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: