str_replace替换它刚刚替换的文本?

时间:2012-09-18 12:33:12

标签: replace str-replace

弄乱了代码。它应该这样做吗?我可以阻止它影响已经替换的文本吗?我试图将匹配的单词转换为显示悬停定义的链接。一切都好,除了我认为定义中的文字也被替换了。

所以文字是...... 成为Chancellor的机会使您能够了解并分享您对预算和青年预算的意见! 我们收集所有参与Chance to Chancellor并在青年预算中公布的预算选择,以便为您 - 未来的纳税人和选民 - 在2013年预算中发表意见。

链接http://www.payingforit.org.uk/chance-to-be-chancellor/youth-budget-2012/46037/

ob_start();
the_content();
$content = ob_get_clean();  
$new_content = str_ireplace($match_term,$match_term_replacement,$content);

数组

matches = Array (
    [0] =>  Youth Budget 
    [1] =>  Welfare system  
    [2] =>  Tax 
    [3] =>  Recession 
    [4] =>  Public spending   
    [5] =>  Public protection 
    [6] =>  PSHEe 
    [7] =>  Pfeg Quality Mark  
    [8] =>  Pension 
    [9] =>  National debt  
    [10] =>  Key Stage 4 
    [11] =>  Income Tax 
    [12] =>  HM Treasury 
    [13] =>  Global financial crisis 
    [14] =>  Gilts 
    [15] =>  Economic awareness 
    [16] =>  Deficit 
    [17] =>  Credit crunch 
    [18] =>  Chancellor 
    [19] =>  Citizenship  
    [20] =>  Budget 
    )


     replace with Array
        (
            [0] =>  <a class="matched_term" href="/about/glossary/#youth-budget"><span class="term">Youth Budget</span><span class="definition">A report from the Citizenship Foundation on the results of all those who take part in Chance to be Chancellor and share their opinions on the Budget of that year.</span></a> 
            [1] =>  <a class="matched_term" href="/about/glossary/#welfare-system"><span class="term">Welfare system </span><span class="definition">A social system where by the Government provides assistance to those individuals and families in need of assistance.</span></a> 
            [2] =>  <a class="matched_term" href="/about/glossary/#tax"><span class="term">Tax</span><span class="definition">A compulsory money contribution to the Government and taken from the producing and selling of goods and services, the earning of income and ownership of property etc.</span></a> 
            [3] =>  <a class="matched_term" href="/about/glossary/#recession"><span class="term">Recession</span><span class="definition">Period of general economic decline defined usually as a lack of economic growth for six months or longer and marked by high unemployment and a drop in retail sales.</span></a> 
            [4] =>  <a class="matched_term" href="/about/glossary/#public-spending"><span class="term">Public spending  </span><span class="definition">Money spent by a government to pay for public services and public infrastructure which is supported by taxation.</span></a> 
            [5] =>  <a class="matched_term" href="/about/glossary/#public-protection"><span class="term">Public protection</span><span class="definition">Is the part of the UK Budget which funds the police, courts and prison system along with emergency services.</span></a> 
            [6] =>  <a class="matched_term" href="/about/glossary/#pshee"><span class="term">PSHEe</span><span class="definition">Personal, social, health and economic education (PSHEe) is a planned programme of learning opportunities and experiences to equip young people with the knowledge, understanding, attitudes and practical skills to live healthily, safely, productively and responsibly.</span></a> 
            [7] =>  <a class="matched_term" href="/about/glossary/#pfeg-quality-mark"><span class="term">Pfeg Quality Mark </span><span class="definition">An accreditation system on financial and economic educational resources from pfeg (Personal Finance Education Group) – an independent charity providing knowledge, resources and support for teaching children about money.</span></a> 
            [8] =>  <a class="matched_term" href="/about/glossary/#pension"><span class="term">Pension</span><span class="definition">A pension is a benefit received by people after they have retired. There are three main types: personal where individuals pay regular amounts to a pension provider, company which are set up by employers and state paid by the Government.</span></a> 
            [9] =>  <a class="matched_term" href="/about/glossary/#national-debt"><span class="term">National debt </span><span class="definition">The sum of all previously incurred annual deficits. Since deficits are financed by government borrowing, national debt is equal to all outstanding government debt.</span></a> 
            [10] =>  <a class="matched_term" href="/about/glossary/#key-stage-4"><span class="term">Key Stage 4</span><span class="definition">The National Curriculum is organised into four 'key stages’. Key Stage 4 is aimed towards pupils in Years 10 and 11 (S3 and S4 in Scotland) usually between the ages of 14 and 16 years old.</span></a> 
            [11] =>  <a class="matched_term" href="/about/glossary/#income-tax"><span class="term">Income Tax</span><span class="definition">A tax paid on income earned by individuals working in the UK.</span></a> 
            [12] =>  <a class="matched_term" href="/about/glossary/#hm-treasury"><span class="term">HM Treasury</span><span class="definition">The UK Government department responsible for public finances and the country’s economic policy.</span></a> 
            [13] =>  <a class="matched_term" href="/about/glossary/#global-financial-crisis"><span class="term">Global financial crisis</span><span class="definition">A situation where the value of financial institutions (e.g. banks) and/or financial products (e.g. mortgages) drops rapidly due to a collapse in confidence in them.</span></a> 
            [14] =>  <a class="matched_term" href="/about/glossary/#gilts"><span class="term">Gilts</span><span class="definition">An investment opportunity issued by the UK Government to borrow money from investors for a defined period of time at a fixed interest rate in order to finance public spending.</span></a> 
            [15] =>  <a class="matched_term" href="/about/glossary/#economic-awareness"><span class="term">Economic awareness</span><span class="definition">Describes individuals’ knowledge and understanding of the economy, particularly how individuals, business and governments shape it and how the economy shapes society.</span></a> 
            [16] =>  <a class="matched_term" href="/about/glossary/#deficit"><span class="term">Deficit</span><span class="definition">A situation in which government spending exceeds the income it is able to collect from taxation.</span></a> 
            [17] =>  <a class="matched_term" href="/about/glossary/#credit-crunch"><span class="term">Credit crunch</span><span class="definition">An economic condition when credit becomes more difficult to obtain due to banks and financial companies becoming more wary of lending money to businesses and individuals.</span></a> 
            [18] =>  <a class="matched_term" href="/about/glossary/#chancellor"><span class="term">Chancellor</span><span class="definition">British Government minister in charge of the HM Treasury department and responsible for economic and financial matters.</span></a> 
            [19] =>  <a class="matched_term" href="/about/glossary/#citizenship"><span class="term">Citizenship </span><span class="definition">A statutory subject in the National Curriculum taught to all pupils aged 11–16 years old in maintained schools in England. Citizenship explores concepts of democracy, justice, rights and responsibilities, identities and diversity.</span></a> 
            [20] =>  <a class="matched_term" href="/about/glossary/#budget"><span class="term">Budget</span><span class="definition">A statement made every year by the government on how it’s going to get the money it needs to spend on the country.</span></a> 
        )

1 个答案:

答案 0 :(得分:0)

好的 - 所以首先用唯一ID替换它然后再次替换。

$args = array('post_type'=>'glossary-term','posts_per_page'=>-1);

$terms = get_posts($args);

ob_start();
the_content();
$content = ob_get_clean();  

foreach ($terms as $term) {
    $match_id[]=' gloss_id_'.$term->ID.' ';
    $match_term[]=' '.$term->post_title.' ';
    $match_term_replacement[] = ' <a class="matched_term" href="/about/glossary/#'.$term->post_name.'"><span class="term">'.$term->post_title.'</span><span class="definition">'.$term->post_content.'</span></a>'.' ';
    $match_term_id_replacement[] = ' gloss_id_'.$term->ID.' ';
}       

$new_content = str_ireplace($match_term,$match_term_id_replacement,$content);
$new_contentb = str_ireplace($match_id,$match_term_replacement,$new_content);

echo $new_contentb;
相关问题