如何让PHP解析器检测/读取字符串中的HTML标记?

时间:2014-07-28 11:02:54

标签: php string html-parsing substr

我有以下PHP代码,我想在其中提取sub-string而不会丢失其中的HTML代码。

  1. 虽然我想在字符串中保留HTML标记,但 PHP 解析器似乎没有读取或检测HTML标记 - 输出 该程序遵循代码。

  2. 其次'似乎被’取代。

  3. 第三,一个小问题:是否有办法存储字符串 一个变量,它包含单引号和双引号 不要逃避引号,就好像我们可以在python 中使用 triple - '一样?

  4. 请指导我为什么会出现这些问题以及我该怎么办?

    <?php
    
    $haystack = '<div class="qtext"><p>^^^^^When children experience emotional upset or trauma, their problems are sometimes made worse by well-meaning adults who either don’t notice their distress or insist that they express themselves through an adult medium: verbal language. Play therapy, on the other hand, recognizes that imaginative play is the mode of expression most comfortable and natural to children and focuses on helping them express their emotions. A form of psychodynamic treatment, play therapy takes for granted the fact that, once aided in expression, children are able to resolve their anxieties themselves.</p>
    <p>The equipment of play therapy is simple and familiar. Dolls, play dishes, soldiers, stuffed animals, and sandboxes all have their place in the play therapy room. The job of the therapist, however, is a bit more challenging, because he or she must refrain from guiding the child in a particular direction or asking pointed questions. The therapist’s task is partly to observe the child’s play and recognize the child’s emotions and help bring them to expression. The second, very important, task is for the therapist to accept the child unconditionally.</p>
    <p>A well-known example of successful play therapy is documented in Dr. Virginia Axline’s book Dibs in Search of Self. Dibs is an emotionally disturbed 5-year-old whose teachers fear he may have brain damage. In his play therapy sessions with Dr. Axline, however, a different picture emerges. Dibs’ play with a dollhouse and several dolls reveals that he feels cut off from his family and that he is angry with them for locking him in his room. This theme is repeated in play with animals in the room’s sandbox; he frees them from captivity again and again. During the sessions, Dr. Axline is careful not to express preference for any of Dibs’ activities, and most of her comments are simply reflecting back to him what he has just said. Additionally, through her choices to help him express his anger rather than offering him comfort in it, Dr. Axline fosters in Dibs a sense of emotional independence. At the end of the case study, Dibs has emerged as a content, intelligent, compassionate child.^^^^^</p>
    <p>According to the passage, which of the following is a reason that play therapists help disturbed children express their emotions?</p></div><div class="ablock"><div class="prompt">Select one:</div>';
    
    $start= strpos( $haystack, '^^^^^' ); //Find the position of the first occurance of vvvvv
    
    $end= strrpos( $haystack, '^^^^^' ) ; //Find the position of the last occurance of vvvvv
    
    $length= strlen($haystack);
    
    $haystack_substring= substr( $haystack, ($start+5), ($end-$start-5) ); //end is the desired length of the returned substring. Since end is our substring of interest plus 5 v's, so we are doing -5 to passed the desired length of our substring
    
    $haystack_substring .= '</p>';
    
    echo $haystack_substring;
    

    输出: -

    When children experience emotional upset or trauma, their problems are sometimes made worse by well-meaning adults who either don’t notice their distress or insist that they express themselves through an adult medium: verbal language. Play therapy, on the other hand, recognizes that imaginative play is the mode of expression most comfortable and natural to children and focuses on helping them express their emotions. A form of psychodynamic treatment, play therapy takes for granted the fact that, once aided in expression, children are able to resolve their anxieties themselves.
    The equipment of play therapy is simple and familiar. Dolls, play dishes, soldiers, stuffed animals, and sandboxes all have their place in the play therapy room. The job of the therapist, however, is a bit more challenging, because he or she must refrain from guiding the child in a particular direction or asking pointed questions. The therapist’s task is partly to observe the child’s play and recognize the child’s emotions and help bring them to expression. The second, very important, task is for the therapist to accept the child unconditionally.
    
    A well-known example of successful play therapy is documented in Dr. Virginia Axline’s book Dibs in Search of Self. Dibs is an emotionally disturbed 5-year-old whose teachers fear he may have brain damage. In his play therapy sessions with Dr. Axline, however, a different picture emerges. Dibs’ play with a dollhouse and several dolls reveals that he feels cut off from his family and that he is angry with them for locking him in his room. This theme is repeated in play with animals in the room’s sandbox; he frees them from captivity again and again. During the sessions, Dr. Axline is careful not to express preference for any of Dibs’ activities, and most of her comments are simply reflecting back to him what he has just said. Additionally, through her choices to help him express his anger rather than offering him comfort in it, Dr. Axline fosters in Dibs a sense of emotional independence. At the end of the case study, Dibs has emerged as a content, intelligent, compassionate child.
    

0 个答案:

没有答案