Немедленный выход из & # 39; в то время как & # 39; цикл в C ++ [закрыто]

<?php
$selfClosing = explode(',', 'area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed');

$html = '
<p><a href="#">foo</a></p>
<hr/>
<br/>
<div>name</div>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$els = $dom->getElementsByTagName('*');
foreach ( $els as $el ) {
    $nodeName = strtolower($el->nodeName);
    if ( !in_array( $nodeName, $selfClosing ) ) {
        var_dump( $nodeName );
    }
}

Выход:

string(4) "html"
string(4) "body"
string(1) "p"
string(1) "a"
string(3) "div"

В основном просто определяют имена узлов узлов, которые закрываются самостоятельно, загружают всю строку html в библиотеку DOM, захватывают все элементы, перебирают и отфильтровывают которые не закрываются и не работают на них.

Я уверен, что вы уже знаете, что вам не следует использовать регулярное выражение для этой цели.

13
задан Peter Mortensen 19 July 2015 в 17:32
поделиться

9 ответов

Use break?

while(choice!=99)
{
  cin>>choice;
  if (choice==99)
    break;
  cin>>gNum;
}
52
ответ дан 1 December 2019 в 17:15
поделиться

Use break, as such:

while(choice!=99)
{
  cin>>choice;
  if (choice==99)
    break; //exit here and don't get additional input
  cin>>gNum;
}

This works for for loops also, and is the keyword for ending a switch clause. More info here.

6
ответ дан 1 December 2019 в 17:15
поделиться
cin >> choice;
while(choice!=99) {
    cin>>gNum;
    cin >> choice
}

You don't need a break, in that case.

8
ответ дан 1 December 2019 в 17:15
поделиться

break;.

while(choice!=99)
{
   cin>>choice;
   if (choice==99)
       break;
   cin>>gNum;
}
3
ответ дан 1 December 2019 в 17:15
поделиться

Yes, break will work. However, you may find that many programmers prefer not to use it when possible, rather, use a conditional if statement to perform anything else in the loop (thus, not performing it and exiting the loop cleanly)

Something like this will achieve what you're looking for, without having to use a break.

while(choice!=99) {
    cin >> choice;
    if (choice != 99) {
        cin>>gNum;
    }
}
3
ответ дан 1 December 2019 в 17:15
поделиться

хм, перерыв ?

2
ответ дан 1 December 2019 в 17:15
поделиться
while(choice!=99)
{
  cin>>choice;
  if (choice==99)
    exit(0);
  cin>>gNum;
}

Trust me, that will exit the loop. If that doesn't work nothing will. Mind, this may not be what you want...

1
ответ дан 1 December 2019 в 17:15
поделиться

Yah Im pretty sure you just put

    break;

right where you want it to exit

like

    if (variable == 1)
    {
    //do something
    }
    else
    {
    //exit
    break;
    }
1
ответ дан 1 December 2019 в 17:15
поделиться

Try

break;
0
ответ дан 1 December 2019 в 17:15
поделиться
Другие вопросы по тегам:

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